Telephone Digits Application Program in Java
Submitted by GeePee on Sunday, May 24, 2015 - 22:32.
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.
Sample Run:
The statement declare the appropriate variables.
The statement 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.
- import javax.swing.JOptionPane;
- public class TelephoneDigits
- {
- {
- char letter;
- String inputMessage;
- String inputString;
- String outputMessage;
- inputMessage = "Program to convert uppercase "
- + "leteers to their corresponding telephone digits.\n"
- + "To stop the program enter #.\n"
- + "ENTER A LETTER: ";
- inputString =
- letter = inputString.charAt(0);
- while (letter != '#')
- {
- outputMessage = "The letter you entered is: " + letter + "\n"
- + "The corresponding telephone digit is: ";
- if (letter >= 'A' && letter <= 'Z')
- {
- switch (letter)
- {
- case 'A':
- case 'B':
- case 'C':
- outputMessage = outputMessage + "2";
- break;
- case 'D':
- case 'E':
- case 'F':
- outputMessage = outputMessage + "3";
- break;
- case 'G':
- case 'H':
- case 'I':
- outputMessage = outputMessage + "4";
- break;
- case 'J':
- case 'K':
- case 'L':
- outputMessage = outputMessage + "5";
- break;
- case 'M':
- case 'N':
- case 'O':
- outputMessage = outputMessage + "6";
- break;
- case 'P':
- case 'Q':
- case 'R':
- case 'S':
- outputMessage = outputMessage + "7";
- break;
- case 'T':
- case 'U':
- case 'V':
- outputMessage = outputMessage + "8";
- break;
- case 'W':
- case 'X':
- case 'Y':
- case 'Z':
- outputMessage = outputMessage + "9";
- }
- }
- else
- outputMessage = outputMessage + "INVALID INPUT";
- inputMessage = "Enter another uppercase letter to find its correspondinf"
- + "telephone digit. \n"
- + "To stop the program enter #.\n"
- + "Enter a letter: ";
- inputString =
- letter = inputString.charAt(0);
- }
- }
- }



- inputMessage = "Program to convert uppercase "
- + "leteers to their corresponding telephone digits.\n"
- + "To stop the program enter #.\n"
- + "ENTER A LETTER: ";
Add new comment
- 90 views