*6.21 (phone by keyboard) the international standard letter / number matching diagram is shown in the programming exercise 4.15. Write a method to return the number of a given capital letter, as follows:
int getNumber(char uppercaseLetter)
Write a test program to prompt the user to enter a phone number in the form of a string. The number you enter may contain letters. The program translates letters (uppercase or lowercase) into a number, and then keeps the other characters unchanged.
*6.21(Phone keypads)The international standard letter/number mapping for telephones is given in Programming Exercise 4.15. Write a method that returns a number, given an uppercase letter, as follows:
int getNumber(char uppercaseLetter)
Write a test program that prompts the user to enter a phone number as a string. The input number may contain letters. The program translates a letter (uppercase or lowercase) to a digit and leaves all other characters intact.
Here is the reference answer code:
// https://cn.fankuiba.com import java.util.Scanner; public class Ans6_21_page201 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a string: "); String phoneNumber = input.nextLine().toUpperCase(); for (int i =0; i < phoneNumber.length(); i++) { if (Character.isDigit(phoneNumber.charAt(i))) { System.out.print(phoneNumber.charAt(i)); } else if (getNumber(phoneNumber.charAt(i)) == 1) System.out.print("-"); else System.out.print(getNumber(phoneNumber.charAt(i))); } } public static int getNumber(char uppercaseLetter) { int num = 99; switch (uppercaseLetter+"") { case "-": num = 1; break; case "A": num = 2; break; case "B": num = 2; break; case "C": num = 2; break; case "D": num = 3; break; case "E": num = 3; break; case "F": num = 3; break; case "G": num = 4; break; case "H": num = 4; break; case "I": num = 4; break; case "J": num = 5; break; case "K": num = 5; break; case "L": num = 5; break; case "M": num = 6; break; case "N": num = 6; break; case "O": num = 6; break; case "P": num = 7; break; case "Q": num = 7; break; case "R": num = 7; break; case "S": num = 7; break; case "T": num = 8; break; case "U": num = 8; break; case "V": num = 8; break; case "W": num = 9; break; case "X": num = 9; break; case "Y": num = 9; break; case "Z": num = 9; } return num; } }
Applicable to Java language programming and data structure (basic part) (the 11th edition of the original book) Java language programming (basic part) (the 10th / 11th edition of the original book)
More