Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1.  
  2. import java.util.HashMap;
  3.  
  4. public class MobilePhoneNumPadToCharacters {
  5.  
  6. public static void main(String[] args) {
  7. System.out.println(returnWords("222#2#8")); //CAT
  8. System.out.println(returnWords("5555#2#888888#2222")); //JAVA
  9. }
  10.  
  11.  
  12. public static String returnWords(String str)
  13. {
  14. HashMap<Character, String> mobileNumPad = new HashMap<Character, String>();
  15.  
  16. mobileNumPad.put('2', "ABC");
  17. mobileNumPad.put('3', "DEF");
  18. mobileNumPad.put('4', "GHI");
  19. mobileNumPad.put('5', "JKL");
  20. mobileNumPad.put('6', "MNO");
  21. mobileNumPad.put('7', "PQRS");
  22. mobileNumPad.put('8', "TUV");
  23. mobileNumPad.put('9', "WXYZ");
  24. mobileNumPad.put('0'," ");
  25.  
  26. String[] tokens = str.split("#");
  27. String out = "";
  28.  
  29. for (int i = 0; i < tokens.length; i++)
  30. {
  31. char temp = tokens[i].charAt(0);
  32. String getWords = mobileNumPad.get(temp);
  33. int length = tokens[i].length();
  34.  
  35. if(length>getWords.length())
  36. {
  37. length = length%getWords.length();
  38. }
  39.  
  40. if(length == 0)
  41. {
  42. length = getWords.length();
  43. }
  44. out = out+getWords.charAt(length-1);
  45.  
  46. }
  47.  
  48. return out;
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement