1. Use if statements to write a Java program that inputs a single letter and pri
ID: 3754194 • Letter: 1
Question
1. Use if statements to write a Java program that inputs a single letter and prints out the corresponding digit on the telephone. The letters and digits on a telephone are grouped this way No digit corresponds to either Q or Z. For these 2 letters your program should print a message indicating that they are not used on a telephone. The program may operate like this (sample output): Enter a single uppercase letter and you will get the corresponding digit on the telephone: R The digit 7 corresponds to the letter R on the telephone Another sample: Enter a single uppercase letter and you will get the corresponding digit on the telephone: Th ere is no digit on the telephone that corresponds to zExplanation / Answer
Java program to print corresponding digit on telephone directory.
-----------------------------------------------------------------------------------------------------------------
import java.util.Scanner; //Importing java.util.Scanner for Scanner class
class telephone {
public static void main(String[] args) { //Main class
Scanner sc = new Scanner(System.in);
System.out.print("Enter a single uppercase letter and you will get the corresponding digit on the telephone :"); //Printing the statement
char s = sc.next().charAt(0); //Taking the input Character from the user
int number =1;
if (s == 'A' || s == 'B' || s == 'C') {
System.out.println("The digit 2 corresponds to the letter "+s+" on the telephone."); //If letter equals to A or B or C return 2
}
else if (s == 'D' || s == 'E' || s == 'F') {
System.out.println("The digit 3 corresponds to the letter "+s+" on the telephone."); //If letter equals to D or E or F return 3
}
else if (s == 'G' || s == 'H' || s == 'I') {
System.out.println("The digit 4 corresponds to the letter "+s+" on the telephone."); //If letter equals to G or H or I return 4
}
else if (s == 'J' || s == 'K' || s == 'L') {
System.out.println("The digit 5 corresponds to the letter "+s+" on the telephone."); //If letter equals to J or K or L return 5
}
else if (s == 'M' || s == 'N' || s == 'O') {
System.out.println("The digit 6 corresponds to the letter "+s+" on the telephone."); //If letter equals to M or N or O return 6
}
else if (s == 'P' || s == 'R' || s == 'S') {
System.out.println("The digit 7 corresponds to the letter "+s+" on the telephone."); //If letter equals to P or R or S return 7
}
else if (s == 'T' || s == 'U' || s == 'V') {
System.out.println("The digit 8 corresponds to the letter "+s+" on the telephone."); //If letter equals to T or U or V return 8
}
else if (s == 'W' || s == 'X' || s == 'Y') {
System.out.println("The digit 9 corresponds to the letter "+s+" on the telephone."); //If letter equals to W or X or Y return 9
}
else if (s == 'Q' || s == 'Z') {
System.out.println("There is no digit on the telephone that corresponds to "+s); //If letter equals to Q or Z return No corresponding digit.
}
else
{
System.out.println("Invalid Character. Enter a valid Character"); //If user a invalid character return error.
}
}
}
-----------------------------------------------------------------------------------------------------------------
Thank you,
With regards.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.