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