Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all, i am unsure how to approach this lookUpNumber method. I need the method to use name as a key to look up the associated number value. If there is no matching name in the HashMap then the method must return null. Here is my class.

What I have tried:

Java
<pre>public class MapTester
{
    // instance variables - replace the example below with your own
    private HashMap<String, String> contacts;

    /**
     * Constructor for objects of class MapTester
     */
    public MapTester()
    {
        contacts = new HashMap<String, String>();
        
    }

    /**
     * Enter the given pair into the HashMap.
     * @param name A contact name (the key).
     * @param number The contact's phone number (as a string) (the value).
     */
    public void enterNumber(String name, String number)
    {
        contacts.put(name, number);
    }
    
    /**
     * Look up the given name in the map and return the associated number.
     * @param name The name to be looked up (the key)
     * @return The assoicated number, if any (the value).
     */
    public String lookUpNumber(String name)
    {
        return null;
    }
}
Posted
Updated 1-Dec-22 4:50am

Use the HashMap.containsKey (Java Platform SE 8 )[^] or HashMap.getOrDefault (Java Platform SE 8 )[^] method.

[edit]
Let's assume that name contains an actual string ...
Java
    public String lookUpNumber(String name)
    {
        String number;
// option 1 - first check if the key exists
        if (contacts.containsKey(name)
        {
            number = (String)contacts.get(name);
        }
// option 2 - get the value in the map or some default value
        number = (String)contacts.getOrDefault(name, "NoName");

// option 3 - just use get which returns null if the key does not exist
        number = (String)contacts.get(name);

        // return whatever value was found
        return number;
    }


[/edit]
 
Share this answer
 
v2
Comments
Freddie Francis 1-Dec-22 11:00am    
ive got this so far. public String lookUpNumber(String name)
{
if(name = ){
get.HashMap();
}
else{
return null;
}
}

But not sure what to include in the if statement
Richard MacCutchan 1-Dec-22 12:23pm    
See my updated solution. You may want to add a check at the beginning that the name is not null on entry.
That's for free: The HashMap container already behaves the required way, see HashMap (Java Platform SE 8 )[^].
 
Share this answer
 
Comments
Freddie Francis 1-Dec-22 10:54am    
ive got this so far. public String lookUpNumber(String name)
{
if(name = ){
get.HashMap();
}
else{
return null;
}
}

But not sure what to include in the if statement
CPallini 1-Dec-22 11:03am    
You don't need any if.
Just write:
public String lookUpNumber(String name)
{
return contacts.get(name);
}
Freddie Francis 1-Dec-22 11:05am    
but i need to method to return null if no matching name
CPallini 1-Dec-22 11:17am    
You didn't read the HashMap.get method documentation I've linked, did you?
Freddie Francis 1-Dec-22 11:36am    
yes i understand, is there also a method for printing out the keys in a hashmap

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900