4. Write a Java application that converts letters of the alphabet into their cor
ID: 3760343 • Letter: 4
Question
4. Write a Java application that converts letters of the alphabet into their corresponding digits on the telephone. The application should let the user enter letters repeatedly until the user responds ''No'' when asked if he or she would like to enter another letter, It should print an error message when any nonalphabetic character is entered. The letters and digits on the telephone have the following correspondence: ABC = 2 DEF = 3 GHI = 4 JKL = 5 MNO = 6 PQRS =7 TUV = 8 WXYZ = 9 Here is an example: When the user enters P, the application displays The letter P corresponds to 7 on the telephone. When the user enters A, the application displays The letter A corresponds to 2 on the telephone. When the user enters D, the application displays The letter D corresponds to 3 on the telephone. When the user enters 2, the application displays Invalid letter.Explanation / Answer
import java.io.*;
import java.lang.*;
class Telephone {
public static void main(String args[]) throws IOException {
String letter;
char aChar;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
letter = br.readLine();
if (letter !=null)
aChar = letter.toUpperCase().charAt(0);
else aChar=' ';
while ((aChar != 'Q') && (aChar != 'Z')) {
if ("ABC".indexOf(aChar) != -1)
Telephone.report(aChar, 2);
else if ("DEF".indexOf(aChar) != -1)
Telephone.report(aChar, 3);
else if ("GHI".indexOf(aChar) != -1)
Telephone.report(aChar, 4);
else if ("JKL".indexOf(aChar) != -1)
Telephone.report(aChar, 5);
else if ("MNO".indexOf(aChar) != -1)
Telephone.report(aChar, 6);
else if ("QRS".indexOf(aChar) != -1)
Telephone.report(aChar, 7);
else if ("TUV".indexOf(aChar) != -1)
Telephone.report(aChar,8);
else if ("WXY".indexOf(aChar) != -1)
Telephone.report(aChar, 9);
else {
System.out.println("Invalid letter.");
}
letter = br.readLine();
if (letter !=null)
aChar = letter.toUpperCase().charAt(0);
else aChar=' ';
aChar = letter.charAt(0);
}
System.out.println();
System.out.println("Have a nice day!");
}
private static void report (char aChar, int value) {
System.out.print("The letter " + aChar + " corresponds to ");
System.out.print(value);
System.out.println(" on the telephone.");
System.out.println();
}
}
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.