I have to design a program that asks the user to enter a 10-digit character tele
ID: 3778841 • Letter: I
Question
I have to design a program that asks the user to enter a 10-digit character telephone number in the format XXX-XXX-XXXX. The program should display the telephone number with any alphabetical characters that appeared in the original translated to their numeric equivalent. Example: if the user enters 555-GET-FOOD the program should display 555-438-3663.
The alaphabetic letters are mapped to numbers here:
A B C = 2, D E F = 3
G H I = 4, J K L = 5
M N O = 6, P Q R S = 7
T U V = 8, W X Y Z = 9
This problem is coming out of my Starting Out With Programming Logic and Design 4th Edition textbook (Chapter 12). I just need to figure out how to convert letters into numbers in my Javascript program. So far, I'm using String functions and arrays. I also need loops, but can't figure out which ones. Please help! I would appreciate it!
Explanation / Answer
import java.util.Scanner;
public class PhoneNumber {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("enter the phone number in the desired format");
String a=s.nextLine();
System.out.println("enter number is : "+a);
int len = a.length();
for (int i = 0; i < len; i++)
{
char c = a.charAt(i);
switch(c)
{
case 'A':
case 'B':
case'C':
System.out.print("2");
break;
case'D':
case'E':
case'F':
System.out.print("3");
break;
case 'G':
case 'H':
case 'I':
System.out.print("4");
break;
case 'J':
case 'K':
case 'L':
System.out.print("5");
break;
case 'M':
case 'N':
case 'O':
System.out.print("6");
break;
case 'P':
case 'Q':
case 'R':
case 'S':
System.out.print("7");
break;
case 'T':
case 'U':
case 'V':
System.out.print("8");
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
System.out.print("9");
break;
default:
System.out.print(c);
}
}
}
}