Many companies use telephone numbers like 555-GET-Food so the number is easier f
ID: 3660899 • Letter: M
Question
Many companies use telephone 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 O = 6 P, Q, R, and S = 7 T, U, and V = 8 W, X, y, and Z = 9 Write an application that asks the user to enter a 10-character telephone number in the format XXX-XXX-XXXX. The application 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 application should display 555-438-3663. Use the ShowMeTheNumber.java file as the demo file containing the main method import java.util.Scanner; /** This program demonstrates a solution to the Alphabetic Telephone Number Translator programming challenge. */ public class ShowMeTheNumber { public static void main(String[] args) { // Create a Scanner object to read // keyboard input. Scanner keyboard = new Scanner(System.in); // Get a phone number. System.out.print("Enter a phone number containing " + "alphabetic characters: "); String phone = keyboard.nextLine(); // Create an instance of the PhoneTranslator class. PhoneTranslator pt = new PhoneTranslator(phone); // Display the numeric version of the number. System.out.println("The numeric version of that number is " + pt.getTranslated()); } }Explanation / Answer
/*100% working code*/
import java.util.Scanner;
class PhoneTranslator {
String phoneNumber;
PhoneTranslator(String str) {
this.phoneNumber = str;
}
String getTranslated() {
boolean status = true;
char[] charPhoneNumber = phoneNumber.toCharArray();
String converted = "";
int value;
if (phoneNumber.length() != 12 || phoneNumber.charAt(3) != '-'
|| phoneNumber.charAt(3) != '-')
status = false;
else {
for (int i = 0; i < charPhoneNumber.length; i++) {
value = charPhoneNumber[i];
if ((value >= 65 && value <= 67)
|| (value >= 97 && value <= 99))
charPhoneNumber[i] = 50;
else if ((value >= 68 && value <= 70)
|| (value >= 100 && value <= 102))
charPhoneNumber[i] = 51;
else if ((value >= 71 && value <= 73)
|| (value >= 103 && value <= 105))
charPhoneNumber[i] = 52;
else if ((value >= 74 && value <= 76)
|| (value >= 106 && value <= 108))
charPhoneNumber[i] = 53;
else if ((value >= 77 && value <= 79)
|| (value >= 109 && value <= 111))
charPhoneNumber[i] = 54;
else if ((value >= 80 && value <= 83)
|| (value >= 112 && value <= 115))
charPhoneNumber[i] = 55;
else if ((value >= 84 && value <= 86)
|| (value >= 116 && value <= 118))
charPhoneNumber[i] = 56;
else if ((value >= 87 && value <= 90)
|| (value >= 109 && value <= 122))
charPhoneNumber[i] = 57;
}
}
if (status == true) {
for (int i = 0; i < charPhoneNumber.length; i++) {
converted = converted + charPhoneNumber[i];
}
return converted;
} else {
String str = "Invalid number";
return str;
}
}
}
public class ShowMeTheNumber {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a phone number containing "
+ "alphabetic characters: ");
String phone = keyboard.nextLine();
PhoneTranslator pt = new PhoneTranslator(phone);
System.out.println("The numeric version of that number is "
+ pt.getTranslated());
}
}
Output:-
Enter a phone number containing alphabetic characters: 555-GET-FOOD
The numeric version of that number is 555-438-3663
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.