Saturday, 20 August 2016

Chapter 6 Exercise 21, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*6.21 (Phone keypads) The international standard letter/number mapping for telephones is shown 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 a sample run of the program:

Enter a string: 1-800-Flowers
1-800-3569377
Enter a string: 1800flowers
18003569377

import java.util.Scanner;
 
 
public class ProgrammingExercise6_21 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  System.out.print("Enter a string: ");
  String s = input.nextLine();
  String s2 = "";
   
  for (int i = 0; i < s.length(); i++) {
    
   int number = getNumber(s.toUpperCase().charAt(i));
   if (number !=0) {
    s2 = s2 +number;
   }else {
    s2 = s2+s.charAt(i);
   }
    
  }
   
  System.out.println(s2);
   
 
 }
 
 static int getNumber(char uppercaseLetter) {
 
  int n;
 
  switch (uppercaseLetter) {
  case 'A':
  case 'B':
  case 'C':
   n = 2;
   break;
 
  case 'D':
  case 'E':
  case 'F':
   n = 3;
   break;
 
  case 'G':
  case 'H':
  case 'I':
   n = 4;
   break;
 
  case 'J':
  case 'K':
  case 'L':
   n = 5;
   break;
 
  case 'M':
  case 'N':
  case 'O':
   n = 6;
   break;
 
  case 'P':
  case 'Q':
  case 'R':
  case 'S':
   n = 7;
   break;
 
  case 'T':
  case 'U':
  case 'V':
   n = 8;
   break;
 
  case 'W':
  case 'X':
  case 'Y':
  case 'Z':
   n = 9;
   break;
 
  default:
   n = 0;
   break;
  }
 
  return n;
 }
 
}

1 comment :