Skip to content

Instantly share code, notes, and snippets.

Created Feb 10, 2012
Embed
What would you like to do?
/**
* @param word String to be turned into T9 code
* @return the T9 code of the input string
*
* Method returns the order of buttons of digipad buttons to be pressed to
* get the input String from a T9 predictive text dictionary.
*
* This method assumes the order of the buttons is as follows and ignores case:
*
* 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
*
* punctuation of any kind is replaced with a " ".
*
*/
public static String wordToSignature(String word){
String code = "";
while(!(word=="")){ //while the word isn't empty
String l = word.substring(0, 1); //first letter of the word
if(l.equalsIgnoreCase("a")||l.equalsIgnoreCase("b")||l.equalsIgnoreCase("c")){
code +="2";
}else if(l.equalsIgnoreCase("d")||l.equalsIgnoreCase("e")||l.equalsIgnoreCase("f")){
code +="3";
}else if(l.equalsIgnoreCase("g")||l.equalsIgnoreCase("h")||l.equalsIgnoreCase("i")){
code +="4";
}else if(l.equalsIgnoreCase("j")||l.equalsIgnoreCase("k")||l.equalsIgnoreCase("l")){
code +="5";
}else if(l.equalsIgnoreCase("m")||l.equalsIgnoreCase("n")||l.equalsIgnoreCase("o")){
code +="6";
}else if(l.equalsIgnoreCase("p")||l.equalsIgnoreCase("q")||l.equalsIgnoreCase("r")||l.equalsIgnoreCase("s")){
code +="7";
}else if(l.equalsIgnoreCase("t")||l.equalsIgnoreCase("u")||l.equalsIgnoreCase("v")){
code +="8";
}else if(l.equalsIgnoreCase("w")||l.equalsIgnoreCase("x")||l.equalsIgnoreCase("y")||l.equalsIgnoreCase("z")){
code +="9";
}else{
code +=" ";
}
if(word.length()>1){
word=word.substring(1,word.length()); //returns the word minus its first letter
}else{
break; // prevent outofbounds exception
}
}
return code;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment