0

I can't seem to find the answer to this - apologies if this question has already been asked.

In the Java code below I'm trying to write a method that returns the numeric values (as a string) that would have been pressed on a mobile keypad to generate the input String e.g. "cab" -> 222.

The HashMap isn't empty, however following the append to the StringBuffer object sbf, when I print wordToSignature("cab") in main, it returns nullnullnull.

The problem appears to be with the word.charAt(i) code. When I test the append without word.charAt(i), it performs the append and the value in the HashMap is printed.

I would be very grateful if someone could indicate where I might be going wrong.

Thanks in advance.

public static String wordToSignature(String word) {

    HashMap <String, String> keypad = new HashMap <String, String>();

    keypad.put("a", "2"); keypad.put("b", "2"); keypad.put("c", "2");
    keypad.put("d", "3"); keypad.put("e", "3"); keypad.put("f", "3");
    keypad.put("g", "4"); keypad.put("h", "4"); keypad.put("i", "4");
    keypad.put("j", "5"); keypad.put("k", "5"); keypad.put("l", "5");
    keypad.put("m", "6"); keypad.put("n", "6"); keypad.put("o", "6");
    keypad.put("p", "7"); keypad.put("q", "7"); keypad.put("r", "7"); keypad.put("s", "7");
    keypad.put("t", "8"); keypad.put("u", "8"); keypad.put("v", "8");
    keypad.put("w", "9");   keypad.put("x", "9");   keypad.put("y", "9"); keypad.put("z", "9");

    StringBuffer sbf = new StringBuffer("");

    for(int i = 0; i < word.length(); i++) {
        sbf.append(keypad.get(word.charAt(i)));
    }
    return sbf.toString();
}
2

2 Answers 2

1

Try this:

HashMap<Character, String> keypad = new HashMap<Character, String>();
keypad.put('a', "2"); keypad.put('b', "2"); keypad.put('c', "2");
// and so on - notice the single quotes!

If you're going to iterate over the characters of a String, then you might as well store Characters and not Strings as keys in the Map.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much, that has solved the problem. Sorry, I am a beginner, but what was the problem?
@user3742439 if this answer solved your problem, please consider accepting it, just click on the check mark to its left ;)
@user3742439 the problem is that when you call word.charAt(i) it returns a char, but the map was using a String as a key. Both types must be consistent, the easiest way to fix this was to also use char as a key.
1

Try the following, instead of using a string in the keypad hashmap use a Char.

public static String wordToSignature(String word) {

    HashMap <Character, String> keypad = new HashMap <Character, String>();

    keypad.put('a', "2"); keypad.put('b', "2"); keypad.put('c', "2");
    keypad.put('d', "3"); keypad.put('e', "3"); keypad.put('f', "3");
    keypad.put('g', "4"); keypad.put('h', "4"); keypad.put('i', "4");
    keypad.put('j', "5"); keypad.put('k', "5"); keypad.put('l', "5");
    keypad.put('m', "6"); keypad.put('n', "6"); keypad.put('o', "6");
    keypad.put('p', "7"); keypad.put('q', "7"); keypad.put('r', "7"); keypad.put('s', "7");
    keypad.put('t', "8"); keypad.put('u', "8"); keypad.put('v', "8");
    keypad.put('w', "9");   keypad.put('x', "9");   keypad.put('y', "9"); keypad.put('z', "9");

    StringBuffer sbf = new StringBuffer("");

    for(int i = 0; i < word.length(); i++) {
        sbf.append(keypad.get(word.charAt(i)));
    }
    return sbf.toString();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.