Standard telephone keypads containe the digits zero through

Standard telephone keypads containe the digits zero through nine. The numbers two through nine each have three letters associated with them. Many people find it difficult to memorize phone numbers, so they use the correspondence between digits and letters to develop seven-letter words that correspond to their phone numbers. For example, a person whose telephone number is 686-2377 might remember it as \"NUMBERS\".

Each seven-letter word or word combination corresponds to exactly one seven-digit telephone number, but a seven-digit number corresponds to many seven-letter words, most of which are not English words. It is possible, for example, that the owner of a barbershop would be pleased to know that the shop\'s telephone number 424-7288 corresponds to \"HAIRCUT\", or a liquor store\'s number 233-7226 corresponds to \"BEERCAN\".

Objective:

Write a program that asks a user to enter a seven-digit phone number and then writes to the file \'numbers.txt\' every possible seven-letter word combination corresponding to that number. There are 2187 (i.e. 37) such combinations. Treat the digits 0& 1 as spaces, and the rest as shown:

1   | [space]
      2   | A B C
      3   | D E F
      4   | G H I
      5   | J K L
      6   | M N O
      7   | P R S
      8   | T U V
      9   | W X Y
      0   | [space]

Solution

import java.util.*;
import java.lang.Object;
import java.io.*;

public class TelephoneNumberWordGenerator

{

static String phoneNumber;
char numberLetters[][] = {
{\'0\',\'0\',\'0\'},{\'1\',\'1\',\'1\'},{\'A\',\'B\',\'C\'},
{\'D\',\'E\',\'F\'},{\'G\',\'H\',\'I\'},{\'J\',\'K\',\'L\'},
{\'M\',\'N\',\'O\'},{\'P\',\'R\',\'S\'},
{\'T\',\'U\',\'V\'},{\'W\',\'X\',\'Y\'}};


static Scanner input = new Scanner (System.in);
private ObjectOutputStream output;
PrintStream printStream;
char[] word = new char [7];

public void openFile()
{
try // open file
{
output = new ObjectOutputStream(
new FileOutputStream ( \"Phonenumber.txt\") );

}// end try
catch ( IOException ioEcception )
{
System.err.println( \"Error opening file.\");
} // end catch
}// end method open file
  
  

public void addFileInfo( )
{
try
{
char[] chars = phoneNumber.toCharArray ();
int [] digit = new int [chars.length];
for (int i = 0; i < chars.length; i++)
{
digit[i] = Integer.parseInt(String.valueOf(chars[i]));
}

printStream = new PrintStream(output);
printStream.println(\"\ \");


for ( int level0 = 0; level0 < 3; level0 ++ )
{
word[0] = numberLetters[digit[0]][level0];

for ( int level1 = 0; level1 < 3; level1 ++ )
{
word[1] = numberLetters[digit[1]][level1];

for ( int level2 = 0; level2 < 3; level2 ++ )
{
word[2] = numberLetters[digit[2]][level2];

for ( int level3 = 0; level3 < 3; level3 ++ )
{
word[3] = numberLetters[digit[3]][level3];

for ( int level4 = 0; level4 < 3; level4 ++ )
{
word[4] = numberLetters[digit[4]][level4];

for ( int level5 = 0; level5 < 3; level5 ++ )
{
word[5] = numberLetters[digit[5]][level5];

for ( int level6 = 0; level6 < 3; level6 ++ )
{
word[6] = numberLetters[digit[6]][level6];
printStream.print(word);

printStream.print(\" \");
}
}
}
}
}
}
}

System.out.println(\"File written.\");
System.exit(1);

}
catch (Exception exception )
{
System.err.println( \"Error writing to file.\");
System.exit(1);
}
}

public void closeFile()
{
try // close file
{
if ( output != null )
output.close();
}// end try
catch (IOException ioException )
{
System.err.println( \"Error closing file.\");
System.exit(1);
}// end catch


}// end method closeFile
  
public static void main(String[] args){
  

   TelephoneNumberWordGenerator tt=new TelephoneNumberWordGenerator();
  
System.out.println(\"Enter a seven-digit telephone number: \");
phoneNumber = input.next();
tt.openFile();
tt.addFileInfo();
tt.closeFile();

}
}

Standard telephone keypads containe the digits zero through nine. The numbers two through nine each have three letters associated with them. Many people find it
Standard telephone keypads containe the digits zero through nine. The numbers two through nine each have three letters associated with them. Many people find it
Standard telephone keypads containe the digits zero through nine. The numbers two through nine each have three letters associated with them. Many people find it

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site