Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a program PhoneKeypad that gets a phone number from the user and replaces

ID: 659182 • Letter: C

Question

Create a program PhoneKeypad that gets a phone number from the user and replaces each letter with a digit using hte international standard letter/number mapping. There should be a static method getNumber that receives a phone number (String) and converts all the letters in the phone number to digits and then returns it. You must use a switch statement in the getNumber method (use the character as an argument).

Use the String and Character classes for this program. Do not use regular experessions. The program should keep running until the user enters quit (in any case). Use the console for input and output.

0 = none; 1 = none; 2 = ABC; 3 = DEF; 4 = GHI; 5 = JKL; 6 = MNO; 7 = PQRS; 8 = TUV; 9 = WXYZ

Sample program run:

Enter a phone number or quit to exit: 1-800-Flowers 1-800-3569377 Enter a phone number or quit to exit: 123x Invalid phone number Enter a phone number or quit to exit: 1800Flowers 1-800-3569377 Enter a phone number or quit to exit: Quit

Explanation / Answer

PhoneKeyPad.java

import java.util.Scanner;

public class PhoneKeyPad {

   public static void main(String[] args) {

       Scanner input = new Scanner(System.in);

       while (true) {

           System.out.println("Enter a phone number or quit to exit");

           String message = input.next();

           if(message.length()<8){

               System.out.println("Invalid Phone Number");

               return;

           }

           if (message.equalsIgnoreCase("quit") || message.equalsIgnoreCase("exit")) {

               return;

           } else {

               System.out.println(getNumber(message));

           }

       }

   }

   public static String getNumber(String message) {

       int result = 0;

       for (int i = 0; i < message.length(); i++) {

           char c = message.charAt(i);

           if (Character.isAlphabetic(c)) {

               c = Character.toUpperCase(c);

               switch (c) {

               case 'A':

               case 'B':

               case 'C':

                   result = 2;

                   break;

               case 'D':

               case 'E':

               case 'F':

                   result = 3;

                   break;

               case 'G':

               case 'H':

               case 'I':

                   result = 4;

                   break;

               case 'J':

               case 'K':

               case 'L':

                   result = 5;

                   break;

               case 'M':

               case 'N':

               case 'O':

                   result = 6;

                   break;

               case 'P':

               case 'Q':

               case 'R':

               case 'S':

                   result = 7;

                   break;

               case 'T':

               case 'U':

               case 'V':

                   result = 8;

                   break;

               case 'W':

               case 'X':

               case 'Y':

               case 'Z':

                   result = 9;

                   break;

               default:

                   throw new RuntimeException("Invalid Phone Number");

               }

               message = message.substring(0, i) + result + message.substring(i + 1);

           }

       }

       return message;

   }

  

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote