fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args)
  11. {
  12. System.out.println("Converted \"fishing\" to " + replaceDtmf("fishing"));
  13. System.out.println("Converted \"kidneys\" to " + replaceDtmf("kidneys"));
  14. System.out.println("Converted \"1-800-AWESOME\" to " + replaceDtmf("1-800-AWESOME"));
  15. }
  16.  
  17. private static final char[] keys = {
  18. '2', '2', '2', // A B C
  19. '3', '3', '3', // D E F
  20. '4', '4', '4', // G H I
  21. '5', '5', '5', // J K L
  22. '6', '6', '6', // M N O
  23. '7', '7', '7', '7', // P Q R S
  24. '8', '8', '8', // T U V
  25. '9', '9', '9', '9' // W X Y Z
  26. };
  27.  
  28. public static String replaceDtmf(String str)
  29. {
  30. if (str == null || str.isEmpty()) {
  31. return str;
  32. }
  33.  
  34. str = str.toUpperCase();
  35. StringBuilder result = new StringBuilder();
  36.  
  37. for (int i = 0; i < str.length(); i++) {
  38. int index = str.charAt(i) - 'A';
  39. if (index >= 0 && index < keys.length) {
  40. result.append( keys[ index ] );
  41. }
  42. else {
  43. result.append( str.charAt(i) );
  44. }
  45. }
  46.  
  47. return result.toString();
  48. }
  49. }
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
Converted "fishing" to 3474464
Converted "kidneys" to 5436397
Converted "1-800-AWESOME" to 1-800-2937663