Created
December 10, 2011 05:24
-
-
Save pawitp/1454642 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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