Question

Write a java program that prompts the user to enter a string corresponding to a phone...

Write a java program that prompts the user to enter a string corresponding to a phone number that may include alphabets. Your program should convert the number to the corresponding digits. For example, if the input is 800-SALIK, the output should be 800-72545. The input string can be of any length, and may only include numbers, small or capital letters, dash(-), and spaces. Display an error message if there are any other characters in the string. You have to use a switch structure in your implementation to find the corresponding number for each character, as shown in the phone keypad in figure-1. Your program should allow both small and capital letters (hint: you may use the method toUpperCase() to convert all characters to capital letters before processing). using java programing language

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

package keypad;
import java.util.Scanner;           //importing Scanner class to read data from user
/*StringBuilder class in this program is used to create a copy of the given string and modify it */
import java.lang.StringBuilder;      

public class Keypad {               //class Keypad(change class name if you want)
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);           //creating object for Scanner class
       String number;                           //String number is used to store the input string
       System.out.print("Enter a String : ");
       number = scan.nextLine().toUpperCase();       //reading String and converting to Uppercase
       /* Since java does not allow to modify a string directly ,we create a new String(newnumber) using StringBuilder - a copy of given string and we modify it */
       StringBuilder newnumber = new StringBuilder(number);  
       int flag = 0;                              
       for(int i = 0 ;i < number.length(); i++){           //iterating through every character of the string
           char c = number.charAt(i);               //creating char to hold the character at index i
           if((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == ' '){  
               /*checks c if it has any charcter other than numbers ,letters ,spaces and dash(-) */
               switch(c){
                   //convert letter to corresponding number
                   case 'A' :
                   case 'B' :
                   case 'C':
                       newnumber.setCharAt(i , '2');
                       break;
                   case 'D' :
                   case 'E' :
                   case 'F' :
                       newnumber.setCharAt(i , '3');
                       break;
                   case 'G' :
                   case 'H' :
                   case 'I' :
                       newnumber.setCharAt(i , '4');
                       break;
                   case 'J' :
                   case 'K' :
                   case 'L' :
                       newnumber.setCharAt(i , '5');
                       break;
                   case 'M' :
                   case 'N' :
                   case 'O' :
                       newnumber.setCharAt(i , '6');
                       break;
                   case 'P' :
                   case 'Q' :
                   case 'R' :
                   case 'S' :
                       newnumber.setCharAt(i , '7');
                       break;
                   case 'T' :
                   case 'U' :
                   case 'V' :
                       newnumber.setCharAt(i , '8');
                       break;
                   case 'W' :
                   case 'X' :
                   case 'Y' :
                   case 'Z' :
                       newnumber.setCharAt(i , '9');
                       break;
                   case '-':
                       newnumber.setCharAt(i , '-');
                       break;
                   case ' ':
                       newnumber.setCharAt(i , ' ');
                       break;
               }
           }
           else {
               System.out.println("Error : Unsupported characters in the String");
               flag = 1;
               break;
           }              
       }
       if(flag == 0){
           System.out.println("Modified String : " +newnumber);
       }      
   }  
}

//Screenshot of Code :

//refer this to check the indentations.

//Sample O/P:

//Hope this helps.Give thumbs up,Thank you!

//Comment if you have any doubt.

Add a comment
Know the answer?
Add Answer to:
Write a java program that prompts the user to enter a string corresponding to a phone...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT