In: Computer Science
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: 2 = ABC 3 = DEF 4 = GHI 5 = JKL 6 = MNO 7 = PRS 8 = TUV 9 = WXY
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 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 letter and you will get the corresponding digit on the telephone: Z There is no digit on the telephone that corresponds to Z NOTE: Your program should accept only uppercase letters (error message for special characters or lowercase letters) 2. Rewrite the program - this time use a switch statement.
// Using If else statement
import java.util.Scanner;
import java.lang.*;
class MyException extends Exception
{ }
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a single letter and you will
get the corresponding digit on the telephone :");
char c = sc.next().charAt(0); // taking character input from
user
try
{
if (!Character.isUpperCase(c))
throw new MyException(); // If char is not in uppercase throw
exception
else
{ // Printing digit corresponds to letter
if(c == 'A' || c == 'B' || c == 'C')
System.out.println("The digit 2 corresponds to the letter " + c + "
on the telephone");
else if(c == 'D' || c == 'E' || c == 'F')
System.out.println("The digit 3 corresponds to the letter " + c + "
on the telephone");
else if(c == 'G' || c == 'H' || c == 'I')
System.out.println("The digit 4 corresponds to the letter " + c + "
on the telephone");
else if(c == 'J' || c == 'K' || c == 'L')
System.out.println("The digit 5 corresponds to the letter " + c + "
on the telephone");
else if(c == 'M' || c == 'N' || c == 'O')
System.out.println("The digit 6 corresponds to the letter " + c + "
on the telephone");
else if(c == 'P' || c == 'R' || c == 'S')
System.out.println("The digit 7 corresponds to the letter " + c + "
on the telephone");
else if(c == 'T' || c == 'U' || c == 'V')
System.out.println("The digit 8 corresponds to the letter " + c + "
on the telephone");
else if(c == 'W' || c == 'X' || c == 'Y')
System.out.println("The digit 9 corresponds to the letter " + c + "
on the telephone");
else if(c == 'Q' || c == 'Z')
System.out.println("There is no digit on the telephone that
corresponds to " + c);
}
}
catch(MyException e)
{
System.out.println("Invalid input " + c);
}
}
}
// Switch statement
import java.util.Scanner;
import java.lang.*;
class MyException extends Exception
{ }
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a single letter and you will
get the corresponding digit on the telephone :");
char c = sc.next().charAt(0);
int n;
try
{
if (!Character.isUpperCase(c))
throw new MyException();
else
{
switch (c)
{
case 'A' :
n = 2;
break;
case 'B' :
n = 2;
break;
case 'C' :
n = 2;
break;
case 'D' :
n = 3;
break;
case 'E' :
n = 3;
break;
case 'F' :
n = 3;
break;
case 'G' :
n = 4;
break;
case 'H' :
n = 4;
break;
case 'I' :
n = 4;
break;
case 'J' :
n = 5;
break;
case 'K' :
n = 5;
break;
case 'L' :
n = 5;
break;
case 'M' :
n = 6;
break;
case 'N' :
n = 6;
break;
case 'O' :
n = 6;
break;
case 'P' :
n = 7;
break;
case 'R' :
n = 7;
break;
case 'S' :
n = 7;
break;
case 'T' :
n = 8;
break;
case 'U' :
n = 8;
break;
case 'V' :
n = 8;
break;
case 'W' :
n = 9;
break;
case 'X' :
n = 9;
break;
case 'Y' :
n = 9;
break;
default:
n = 0;
System.out.println("There is no digit on the telephone that
corresponds to "+ c);
break;
}
if (n != 0)
System.out.println("The digit "+ n +" corresponds to the letter "+
c +" on the telephone");
}
}
catch(MyException e)
{
System.out.println("Invalid input " + c);
}
}
}