Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAlex Stephens
13,342 Pointsstuck on Java Data structures using a map to store contact methods task 3 of 3
i'm stuck on on this code challenge but i don't know what to do for that last part it's asking to return the value of the passed in methodName so i tried this
public String getContactInfo(String methodName) {
// FIXME: return the value for the passed in *methodName*
return methodName;
}
but when i clicked check my work it is saying Bummer! I called addContactMethod with "pager" and "(222) 222-2222", and expected it to be added to mContactMethods, but it was not.
so how would i go about doing this task ? any help would be nice.
package com.example.model;
import java.util.Map;
import java.util.Set;
import java.util.HashMap;
public class Contact {
private String mFirstName;
private String mLastName;
private Map<String, String> mContactMethods;
public Contact(String firstName, String lastName) {
mFirstName = firstName;
mLastName = lastName;
/* This stores contact methods by name
* eg: "phone" => "(555) 555-1234"
*/
mContactMethods = new HashMap<String, String>();
}
public void addContactMethod(String method, String value) {
mContactMethods.put(method, value);
}
/**
* Returns the available contact methods. eg: phone, pager,
*
* @return The name of the contact methods that are available
*/
public Set<String> getAvailableContactMethods() {
// FIXME: This should return the current contact method names.
Set keys = mContactMethods.keySet();
return keys;
}
/**
* Returns the value for the contact method if it exists,
*
* @param methodName The name of the contact method to look up.
* @return The name of the contact methods that are available
*/
public String getContactInfo(String methodName) {
return null; // What would i need to do for this part ?
}
public String getFirstName() {
return mFirstName;
}
public String getLastName() {
return mLastName;
}
}
3 Answers
Dan Johnson
40,533 PointsThe methodName parameter represents a key in the mContactMethods map. You can get values with a key with the get method.
Brody Ricketts
15,612 PointsI wrote this up a bit ago in response to another thread about this challenge.
I hope this helps if you're still a bit confused.
// Task 3 "For this task, let's fix getContactInfo method."
public String getContactInfo(String methodName) {
/*
* FIXME: return the value for the passed in *methodName*
*
* The task here is the pull the value from a stored key(contact method).
* Remember when pulling from a HashMap we can use the method .get();
* We know that the name of the HashMap is mContactMethods(check line 10 and 18)
* We also know that the syntax looks much like mMapName.get();
* Maps 4:05
*
* A simple analogy that I hope helps you complete this task:
* You have a bowl from fruit on a table(mFruitBowl), you want to GET a piece of fruit(getFruit();).
* You decide to GET an apple. Apple has become your parameter,
* Remember you have to pass apple into mFruitBowl.get();, because that is where its stored, much like a HashMap.
*/
return null;
}
Alex Stephens
13,342 PointsThanks for your help I have passed the code challenge and thank u for posting that code now I understand it a bit more.☺
Shadd Anderson
Treehouse Project ReviewerPoints to Brody for the excellent analogy! :)
mongst
10,841 PointsThis is what I used to solve:
html
package com.example.model;
import java.util.Map;
import java.util.Set;
import java.util.HashMap;
public class Contact {
private String mFirstName;
private String mLastName;
private Map<String, String> mContactMethods;
public Contact(String firstName, String lastName) {
mFirstName = firstName;
mLastName = lastName;
/* This stores contact methods by name
* eg: "phone" => "(555) 555-1234"
*/
mContactMethods = new HashMap<String, String>();
}
public void addContactMethod(String method, String value) {
mContactMethods.put(method, value);
}
/**
* Returns the available contact methods. eg: phone, pager,
*
* @return The name of the contact methods that are available
*/
public Set<String> getAvailableContactMethods() {
// FIXME: This should return the current contact method names.
Set keys = mContactMethods.keySet();
return keys;
}
/**
* Returns the value for the contact method if it exists,
*
* @param methodName The name of the contact method to look up.
* @return The name of the contact methods that are available
*/
public String getContactInfo(String methodName) {
return mContactMethods.get(methodName);
}
public String getFirstName() {
return mFirstName;
}
public String getLastName() {
return mLastName;
}
}
Alex Stephens
13,342 PointsAlex Stephens
13,342 Pointsso could you give me an example in code so i can get a better understanding.
Dan Johnson
40,533 PointsDan Johnson
40,533 PointsThis is all there is to it:
mContactMethods.get(methodName);
That will get you your return value.