Can't get the correct program.
To make telephone numbers easier to remember, some companies use letters to show their telephone number. For example, the telephone number 438-5626 can be shown as GET-LOAN. In some cases, to make a telephone number
meaningful, companies might use more than seven letters. For example,225-5466 can be displayed as CALL-HOME, which uses eight letters. Write a program that prompts the user to enter a telephone number expressed in letters and outputs the corresponding telephone number in digits. If the user enters more than eight letters, then process only the first seven letters. Also, output the – (hyphen) after the third digit. Allow the user to use uppercase
and lowercase letters, as well as spaces between words. Moreover, your program should process as many telephone numbers as the user wants.
(Hint: You can read the entered telephone number as a string and then use the charAt method of the class String to extract each character.
For example, if str refers to a string, then the expression str.charAt(i) returns the character at the ith position.
Recall that in a string, the position of the first character is 0.)
Always ask the user if another set of values will be entered.
----------------------------------------------------------------------------------------------------------
Program:
import java.util.Scanner;
public class Exam_TelephoneNumber{
public static void main(String [] args) throws Exception
{
Scanner console = new Scanner (System.in);
String answer;
char intAns;
System.out.println("============================== =================================================" );
System.out.println("About: Outputs the equivalent telephone number in letters to its corresponding\ntelephone digit.");
System.out.println("============================== =================================================" );
System.out.println("");
System.out.println("\t\t\t\t* TELEPHONE DIGITS *");
System.out.println("");
do{
System.out.print("Enter the telephone number (in letters): ");
String telNum = console.nextLine();
char let1 = telNum.charAt(0);
char let2 = telNum.charAt(1);
char let3 = telNum.charAt(2);
char let4 = telNum.charAt(3);
char let5 = telNum.charAt(4);
char let6 = telNum.charAt(5);
char let7 = telNum.charAt(6);
char let8 = telNum.charAt(7);
String num1 = "", num2 = "", num3 = "", num4 = "", num5 = "", num6 = "", num7 = "", num8 = "";
switch (let1)
{
case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num1 = "2"; break;
case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num1 = "3"; break;
case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num1 = "4"; break;
case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num1 = "5"; break;
case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num1 = "6"; break;
case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num1 = "7"; break;
case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num1 = "8"; break;
case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num1 = "9"; break;
}
switch (let2)
{
case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num2 = "2"; break;
case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num2 = "3"; break;
case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num2 = "4"; break;
case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num2 = "5"; break;
case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num2 = "6"; break;
case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num2 = "7"; break;
case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num2 = "8"; break;
case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num2 = "9"; break;
}
switch (let3)
{
case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num3 = "2"; break;
case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num3 = "3"; break;
case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num3 = "4"; break;
case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num3 = "5"; break;
case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num3 = "6"; break;
case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num3 = "7"; break;
case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num3 = "8"; break;
case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num3 = "9"; break;
}
switch (let4)
{
case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num4 = "2"; break;
case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num4 = "3"; break;
case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num4 = "4"; break;
case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num4 = "5"; break;
case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num4 = "6"; break;
case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num4 = "7"; break;
case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num4 = "8"; break;
case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num4 = "9"; break;
}
switch (let5)
{
case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num5 = "2"; break;
case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num5 = "3"; break;
case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num5 = "4"; break;
case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num5 = "5"; break;
case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num5 = "6"; break;
case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num5 = "7"; break;
case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num5 = "8"; break;
case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num5 = "9"; break;
}
switch (let6)
{
case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num6 = "2"; break;
case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num6 = "3"; break;
case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num6 = "4"; break;
case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num6 = "5"; break;
case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num6 = "6"; break;
case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num6 = "7"; break;
case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num6 = "8"; break;
case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num6 = "9"; break;
}
switch (let7)
{
case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num7 = "2"; break;
case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num7 = "3"; break;
case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num7 = "4"; break;
case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num7 = "5"; break;
case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num7 = "6"; break;
case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num7 = "7"; break;
case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num7 = "8"; break;
case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num7 = "9"; break;
}
switch (let8)
{
case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num8 = "2"; break;
case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num8 = "3"; break;
case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num8 = "4"; break;
case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num8 = "5"; break;
case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num8 = "6"; break;
case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num8 = "7"; break;
case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num8 = "8"; break;
case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num8 = "9"; break;
}
System.out.println("");
System.out.println("The number is: " + num1 + num2 + num3 + "-" + num4 + num5 + num6 + num7 + num8);
System.out.println("");
System.out.print("Do you want to input another integer (Y - yes / No - any key): ");
answer = console.next();
intAns = answer.charAt(0);
System.out.println("");
}while (intAns == 'Y' || intAns == 'y');
System.out.println("============================== =================================================" );
System.out.println("\t\t\t\t* END OF PROGRAM *");
System.out.println("============================== =================================================" );
}
}
----------------------------------------------------------------------------------------------------------
Output:
* TELEPHONE DIGITS *
Enter the telephone number (in letters): GOTO hell
The number is: 468-6435
Do you want to input another integer (Y - yes / No - any key): y
Enter the telephone number (in letters): GO TOHELL
The number is: 46-86435 <--- this should be 468-6435
Do you want to input another integer (Y - yes / No - any key): n
================================================== =============================
* END OF PROGRAM *
================================================== =============================
Process completed.