Question

In: Computer Science

To make telephone numbers easier to remember, some companies use letters to show their telephone number....

To make telephone numbers easier to remember, some companies use letters to show their telephone number. For example, using letters, 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.

Instructions

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 seven letters, then process only the first seven letters.

Also output the - (hyphen) after the third digit.

Allow the user to use both uppercase and lowercase letters as well as spaces between words.

Moreover, your program should process as many telephone numbers as the user wants.

Use the dialpad below for reference:

The program should accept input and produce output similar to the example program execution below.

Enter Y/y to convert a telephone number from
letters to digits.
Enter any other letter to terminate the program.
Y
Enter a telephone number using letters: Hello world

The corresponding telephone number is:
435-5696
To process another telephone number, enter Y/y
Enter any other letter to terminate the program.
z

Solutions

Expert Solution

import java.util.*;
public class Main
{
   public static void main(String[] args) {
   Scanner sc=new Scanner(System.in);
   char Arr[][]=new char[8][4];

   Arr[0][0]='A';
   Arr[0][1]='B';
   Arr[0][2]='C';
  
   Arr[1][0]='D';
   Arr[1][1]='E';
   Arr[1][2]='F';
  
   Arr[2][0]='G';
   Arr[2][1]='H';
   Arr[2][2]='I';
  
   Arr[3][0]='J';
   Arr[3][1]='K';
   Arr[3][2]='L';
  
   Arr[4][0]='M';
   Arr[4][1]='N';
   Arr[4][2]='O';
  
   Arr[5][0]='P';
   Arr[5][1]='Q';
   Arr[5][2]='R';
   Arr[5][3]='S';

   Arr[6][0]='T';
   Arr[6][1]='U';
   Arr[6][2]='V';

   Arr[7][0]='W';
   Arr[7][1]='X';
   Arr[7][2]='Y';
   Arr[7][3]='Z';
  
   System.out.println("Enter Y/y to convert a telephone number from letters to digits.\nEnter any other letter to terminate the program.");
   char x=sc.next().charAt(0);
if(x=='y'||x=='Y')
{
   System.out.println("Enter a telephone number using letters:");
}
while(x=='y'||x=='Y')
{sc.nextLine();
   String str=sc.nextLine();

   str = str.replaceAll("\\s", "");

str=str.toUpperCase();

   str=shorten(str,7);

  
   int num=0;
   for(int i=0;i<7;i++)
   {
   for(int j=0;j<8;j++)
   {
   for(int k=0;k<4;k++)
   {
   if(str.charAt(i)==Arr[j][k])
   {num=num*10+j+2;}
   }
   }
   }
   String res=Integer.toString(num);
   res=res.substring(0,3)+"-"+res.substring(3,7);
System.out.println("The corresponding telephone number is:"+res);
  
   System.out.println("To process another telephone number, enter Y/y \nEnter any other letter to terminate the program.");
   x=sc.next().charAt(0);
}
   }
   public static String shorten(String s,int len)
   {
   if (s == null)
return null;
if (s.length() > len)
s = s.substring(0, len) + "";
return s;
   }
}


Related Solutions

Alphabetic Telephone Number Translator           Many companies use telephone numbers like 555-GET-FOOD so the number is...
Alphabetic Telephone Number Translator           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            ...
Write pseudocode for a function that translates a telephone number with letters in it (such as...
Write pseudocode for a function that translates a telephone number with letters in it (such as 1-800-FLOWERS) into the actual phone number. Use the standard letters on a phone pad
In PYTHON On a standard telephone, alphabetic letters are mapped to numbers in the following fashion:...
In PYTHON On a standard telephone, 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, and R = 7 T, U, and V = 8 W, X, Y, and Z = 9 Write a fruitful function that takes any string of alphanumeric characters and returns a string...
(Tokenizing Telephone Numbers) Write a program that inputs a telephone number as a string in the...
(Tokenizing Telephone Numbers) Write a program that inputs a telephone number as a string in the form (555) 555-5555. The program should use function strtok to extract the area code as a token, the first three digits of the telephone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. The program should convert the area-code string to int and convert...
Tokenizing Telephone Numbers Write java application that inputs a telephone number as a string in the...
Tokenizing Telephone Numbers Write java application that inputs a telephone number as a string in the form (555) 555-5555. The application should use String method split to extract the area code as a token, the first three digits of the phone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. Both the area code and the phone number should be...
•• P11.1 Phone numbers and PIN codes can be easier to remember when you find words...
•• P11.1 Phone numbers and PIN codes can be easier to remember when you find words that spell out the number on a standard phone keypad. For example, instead of remembering the combination 2633, you can just think of CODE. Write a recursive function that, given a number, yields all possible spellings (which may or may not be real words). •• P11.2 Continue Exercise P11.1, checking the words against the /usr/share/dict/words file on your computer, or the words.txt file in...
Modify Java program to accept a telephone number with any numberof the letters, using the...
Modify Java program to accept a telephone number with any number of the letters, using the if else loop. The output should display a hyphen after the first 3 digits and subsequently a hyphen (-) after every four digits. Also, modify the program to process as many telephone numbers as the user wants.
1. Use cardinality to show that between any two rational numbers there is an irrational number....
1. Use cardinality to show that between any two rational numbers there is an irrational number. Hint: Given rational numbers a < b, first show that [a,b] is uncountable. Now use a proof by contradiction. 2. Let X be any set. Show that X and P(X) do not have the same cardinality. Here P(X) denote the power set of X. Hint: Use a proof by contradiction. If a bijection:X→P(X)exists, use it to construct a set Y ∈P(X) for which Y...
Use cardinality to show that between any two rational numbers there is an irrational number. Hint:...
Use cardinality to show that between any two rational numbers there is an irrational number. Hint: Given rational numbers a < b, first show that [a, b] is uncountable. Now use a proof by contradiction
Please show all steps and write neatly. Make sure the proving is with letters and not...
Please show all steps and write neatly. Make sure the proving is with letters and not with numbers. Please!!!! Thank you Let A, B, C be 3 x 3 matrices. Show (AB)C= A(BC)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT