user3742439
user3742439

Reputation: 61

StringBuffer - append using charAt returns null

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();
}

Upvotes: 0

Views: 594

Answers (2)

Joe
Joe

Reputation: 86

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();
}

Upvotes: 1

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236004

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.

Upvotes: 1

Related Questions