Write a test program that prompts the user to enter a phone

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:

Solution

PhoneNumber.java

import java.util.Scanner;

public class PhoneNumber {

   public static void main(String[] args) {
      
       //Declaring variables
       String str,phoneNum=\"\";
       char ch1;
       int charToNum = 0;
      
       //Scanner object is used to get the inputs entered by the user
               Scanner sc=new Scanner(System.in);
      
               //Getting the input entered by the user
               System.out.print(\"Enter the phone number :\");
               str=sc.next();
              
               /* take character of the phone number,if it is character convert into digit
               * or if it is digit or other character simply add to phone number
               */
               for(int i=0;i<str.length();i++)
               {
                 
               if(Character.isDigit(str.charAt(i)))
               phoneNum+=str.charAt(i);
               else
               {
                   ch1= Character.toUpperCase(str.charAt(i));
                   if(ch1==\'A\'||ch1==\'B\'||ch1==\'C\')
                   {
                       charToNum=2;
                       phoneNum+=charToNum;
                   }
                   else if(ch1==\'D\'||ch1==\'E\'||ch1==\'F\')
                   {
                   charToNum=3;
                   phoneNum+=charToNum;
                   }
                   else if(ch1==\'G\'||ch1==\'H\'||ch1==\'I\')
                   {
                   charToNum=4;
                   phoneNum+=charToNum;
                   }
                   else if(ch1==\'J\'||ch1==\'K\'||ch1==\'L\')
                   {
                   charToNum=5;
                   phoneNum+=charToNum;
                   }
                   else if(ch1==\'M\'||ch1==\'N\'||ch1==\'O\')
                   {
                   charToNum=6;
                   phoneNum+=charToNum;
                   }
                   else if(ch1==\'P\'||ch1==\'Q\'||ch1==\'R\'||ch1==\'S\')
                   {
                   charToNum=7;
                   phoneNum+=charToNum;
                   }
                   else if(ch1==\'T\'||ch1==\'U\'||ch1==\'V\')
                   {
                   charToNum=8;
                   phoneNum+=charToNum;
                   }
                   else if(ch1==\'W\'||ch1==\'X\'||ch1==\'Y\'||ch1==\'Z\')
                   {
                   charToNum=9;
                   phoneNum+=charToNum;
                   }
                   else
                   {
                       ch1=str.charAt(i);
                       phoneNum+=ch1;
                   }
                  
                  
               }     
               }
              
               //Displaying the phone number
               System.out.println(\"Your Phone Number :\"+phoneNum);

   }

}

________________________

Output# 1:

Enter the phone number :1800flowers
Your Phone Number :18003569377

Output#2:

Enter the phone number :1-800-Flowers
Your Phone Number :1-800-3569377

_____________Thank You

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
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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site