Skip to content

Instantly share code, notes, and snippets.

@pawitp
Created December 10, 2011 05:24
Show Gist options
  • Save pawitp/1454642 to your computer and use it in GitHub Desktop.
Save pawitp/1454642 to your computer and use it in GitHub Desktop.
// You'll want to load this from resource
private static char[][] T9_MAP = new char[][] {
{ '0' }, // Default
{ '1' },
{ '2', 'a', 'b', 'c' },
{ '3', 'd', 'e', 'f' },
{ '4', 'g', 'h', 'i' },
{ '5', 'j', 'k', 'l' },
{ '6', 'm', 'n', 'o' },
{ '7', 'p', 'q', 'r', 's' },
{ '8', 't', 'u', 'v'},
{ '9', 'w', 'x', 'y', 'z'}
};
private static String nameToNumber(String name) {
StringBuilder sb = new StringBuilder();
int len = name.length(); // to devatwork: don't inline this, it's faster this way
for (int i = 0; i < len; i++) {
boolean matched = false;
char ch = Character.toLowerCase(name.charAt(i));
for (char[] row : T9_MAP) {
for (char a : row) {
if (ch == a) {
matched = true;
sb.append(row[0]);
break;
}
}
if (matched) {
break;
}
}
if (!matched) {
sb.append(T9_MAP[0][0]);
}
}
return sb.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment