Telephone Digits Application Program in Java

The following program reads the letter codes ‘A’ through ‘Z’ and prints the corresponding telephone digit. . I will be using the JCreator IDE in developing the program. To start in this tutorial, first open the JCreator IDE, click new and paste the following code.
  1. import javax.swing.JOptionPane;
  2.  
  3. public class TelephoneDigits
  4. {
  5. public static void main (String[] args)
  6. {
  7. char letter;
  8.  
  9. String inputMessage;
  10. String inputString;
  11. String outputMessage;
  12.  
  13. inputMessage = "Program to convert uppercase "
  14. + "leteers to their corresponding telephone digits.\n"
  15. + "To stop the program enter #.\n"
  16. + "ENTER A LETTER: ";
  17.  
  18. inputString =
  19. JOptionPane.showInputDialog(inputMessage);
  20.  
  21. letter = inputString.charAt(0);
  22.  
  23. while (letter != '#')
  24. {
  25. outputMessage = "The letter you entered is: " + letter + "\n"
  26. + "The corresponding telephone digit is: ";
  27.  
  28. if (letter >= 'A' && letter <= 'Z')
  29. {
  30. switch (letter)
  31. {
  32. case 'A':
  33. case 'B':
  34. case 'C':
  35. outputMessage = outputMessage + "2";
  36. break;
  37. case 'D':
  38. case 'E':
  39. case 'F':
  40. outputMessage = outputMessage + "3";
  41. break;
  42. case 'G':
  43. case 'H':
  44. case 'I':
  45. outputMessage = outputMessage + "4";
  46. break;
  47. case 'J':
  48. case 'K':
  49. case 'L':
  50. outputMessage = outputMessage + "5";
  51. break;
  52. case 'M':
  53. case 'N':
  54. case 'O':
  55. outputMessage = outputMessage + "6";
  56. break;
  57. case 'P':
  58. case 'Q':
  59. case 'R':
  60. case 'S':
  61. outputMessage = outputMessage + "7";
  62. break;
  63. case 'T':
  64. case 'U':
  65. case 'V':
  66. outputMessage = outputMessage + "8";
  67. break;
  68. case 'W':
  69. case 'X':
  70. case 'Y':
  71. case 'Z':
  72. outputMessage = outputMessage + "9";
  73.  
  74. }
  75. }
  76. else
  77. outputMessage = outputMessage + "INVALID INPUT";
  78. JOptionPane.showMessageDialog(null, outputMessage, "TelephoneDigit",
  79. JOptionPane.PLAIN_MESSAGE);
  80. inputMessage = "Enter another uppercase letter to find its correspondinf"
  81. + "telephone digit. \n"
  82. + "To stop the program enter #.\n"
  83. + "Enter a letter: ";
  84.  
  85. inputString =
  86. JOptionPane.showInputDialog(inputMessage);
  87. letter = inputString.charAt(0);
  88. }
  89. System.exit(0);
  90. }
  91. }
Sample Run: The statement
  1. char letter;
  2.  
  3. String inputMessage;
  4. String inputString;
  5. String outputMessage;
declare the appropriate variables. The statement
  1. inputMessage = "Program to convert uppercase "
  2. + "leteers to their corresponding telephone digits.\n"
  3. + "To stop the program enter #.\n"
  4. + "ENTER A LETTER: ";
create the appropriate string to be included in the input dialog box. The expression in the while statement checks that the letter is not #. If the letter entered by the user is not #, the body of the while loop executes. The if statement checks whether the lettered entered by the user is uppercase. If the lettered entered is uppercase, the logical expression in the if statements evaluates to true and the switch statement executes, which determines the appropriate telephone digit and appends the telephone digit to output message. If the letter entered is not uppercase, the else statement executes and appends the string “INVALID INPUT” to output Message.

Add new comment