Many companies use telphone numbers like 555-GET-FOOD so the number is easier fo
ID: 3556638 • Letter: M
Question
Many companies use telphone numbers like 555-GET-FOOD so the number is easier for their customers to remember. On a standard telephone, the alphabetic letters are mapped to numbers in the following fashion
A, B, and C = 2
D, E, and F = 3
G, H, and I = 4
J, K, and L = 5
M, N, and L = 6
P, Q, R, and S = 7
T, U, and V = 8
W, X, Y, and Z = 9
Design a program that asks the user to enter a 10-character telephone number in the format XXX-XXX-XXXX. The program should display the telephone number with any alphabetic characters that appeared in the original translated to their numeric equivalent. For example, if the user enters 555-GET-FOOD the program should display 555-438-3663.
The design should be in modules. The first line of the function is:
Function String translatePhoneNumber( String Val phoneNumber )
main() will display the value returned from the function.
Explanation / Answer
import java.util.*;
public class Telephone
{public static void main(String[] args)
{String number;
int i;
System.out.print("Enter phone number: ");
Scanner in = new Scanner(System.in);
number = in.next();
i=0;
while(i<number.length())
{switch(number.charAt(i))
{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 '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':
System.out.print("9");
break;
default:
System.out.print(number.charAt(i));
}
i++;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.