Using char type and charAt() method for reading a character from the console Compile and run the below program that runs
Posted: Tue Jul 12, 2022 8:22 am
Using char type and charAt() method for reading acharacter from the console
Compile and run the below program that runs theinternational standard letter to number mapping found on thetelephone (shown below) by reading a letter and displaying itscorresponding digit.
Take notes of the program.
Test the program by entering different characters. and includein your comments:
Note toUpperCase()method converting the input to upper caseletters.
Note if the user input more than one letter, note whatcharAt(0)do.
4. Convert the switch-caseconstruct to an if-else construct in the program
5. Submit your work.
1
2ABC
3DEF
4GHI
5JKL
6MNO
7PQRS
8TUV
9WXYZ
0
// Program # 1
import java.util.Scanner;
public class Program1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a letter: ");
char ch = input.nextLine().charAt(0);
int number = 0;
switch (Character.toUpperCase(ch))
{
case 'A':
case 'B':
case 'C': number = 2; break;
case 'D':
case 'E':
case 'F': number = 3; break;
case 'G':
case 'H':
case 'I': number = 4; break;
case 'J':
case 'K':
case 'L': number = 5; break;
case 'M':
case 'N':
case 'O': number = 6; break;
case 'P':
case 'Q':
case 'R':
case 'S': number = 7; break;
case 'T':
case 'U':
case 'V': number = 8; break;
case 'W':
case 'X':
case 'Y':
case 'Z': number = 9; break;
default: System.out.println(ch + "is an invalid input ");
System.exit(1);
}
System.out.println("The corresponding number is " + number);
}
}
Compile and run the below program that runs theinternational standard letter to number mapping found on thetelephone (shown below) by reading a letter and displaying itscorresponding digit.
Take notes of the program.
Test the program by entering different characters. and includein your comments:
Note toUpperCase()method converting the input to upper caseletters.
Note if the user input more than one letter, note whatcharAt(0)do.
4. Convert the switch-caseconstruct to an if-else construct in the program
5. Submit your work.
1
2ABC
3DEF
4GHI
5JKL
6MNO
7PQRS
8TUV
9WXYZ
0
// Program # 1
import java.util.Scanner;
public class Program1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a letter: ");
char ch = input.nextLine().charAt(0);
int number = 0;
switch (Character.toUpperCase(ch))
{
case 'A':
case 'B':
case 'C': number = 2; break;
case 'D':
case 'E':
case 'F': number = 3; break;
case 'G':
case 'H':
case 'I': number = 4; break;
case 'J':
case 'K':
case 'L': number = 5; break;
case 'M':
case 'N':
case 'O': number = 6; break;
case 'P':
case 'Q':
case 'R':
case 'S': number = 7; break;
case 'T':
case 'U':
case 'V': number = 8; break;
case 'W':
case 'X':
case 'Y':
case 'Z': number = 9; break;
default: System.out.println(ch + "is an invalid input ");
System.exit(1);
}
System.out.println("The corresponding number is " + number);
}
}