Telephone Word Generator: 1. Write a program that, given a seven-digit phone num
ID: 3569123 • Letter: T
Question
Telephone Word Generator:
1. Write a program that, given a seven-digit phone number, uses a PrintStream object to write to a
file every possible seven-letter word combination corresponding to that number. Avoid using
digits 0 and 1 in the phone number. There should be >2000 combinations for each phone
number.
2. Check each combination against any list of common English words, then output to a file only the
combinations that include a English word of at least 6 characters (such as FLOWERS), or include
exactly two English words (TOBEACH). You can find many such word lists available on Internet,
such as http://www.curlewcommunications.co.uk/wordlist.html.
I have attempted to write this program with a friends help and have comeup with the current code:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Phone extends JFrame
{
private int phoneNumber[];
private JTextField input;
private JLabel prompt;
public Phone()
{
super( "Phone" );
input = new JTextField( 15 );
input.addActionListener(
new ActionListener() { // anonymous inner class
public void actionPerformed( ActionEvent event )
{
calculate(); // calculate character sequences
}
} // end anonymous inner class
); // end call to addActionListener
prompt = new JLabel(
"Enter phone number (digits greater than 1 only):" );
Container container = getContentPane();
container.setLayout( new FlowLayout() );
container.add( prompt );
container.add( input );
setSize( 300, 100 );
setVisible( true );
}
// output letter combinations to file
private void calculate()
{
String letters[][] = { { "" },
{ "" }, { "A", "B", "C" }, { "D", "E", "F" },
{ "G", "H", "I" }, { "J", "K", "L" }, { "M", "N", "O" },
{ "P", "R", "S" }, { "T", "U", "V" }, { "W", "X", "Y" } };
long phoneNumber = Long.parseLong( input.getText() );
int digits[] = new int[ 7 ];
for ( int i = 0; i < 7; i++ ) {
digits[i] = ( int )(phoneNumber % 10);
phoneNumber /= 10;
}
PrintStream output = null;
try {
output = new PrintStream( new FileOutputStream( "phone.dat" ) );
}
catch( IOException exception ) {
JOptionPane.showMessageDialog( null, exception.toString(),
"Exception", JOptionPane.ERROR_MESSAGE );
System.exit( 1 );
}
input.setText( "Please wait..." );
int loop[] = new int[ 7 ];
// output all possible combinations
for ( loop[ 0 ] = 0; loop[ 0 ] <= 2; loop[ 0 ]++ )
for ( loop[ 1 ] = 0; loop[ 1 ] <= 2; loop[ 1 ]++ )
for ( loop[ 2 ] = 0; loop[ 2 ] <= 2; loop[ 2 ]++ )
for ( loop[ 3 ] = 0; loop[ 3 ] <= 2; loop[ 3 ]++ )
for ( loop[ 4 ] = 0; loop[ 4 ] <= 2; loop[ 4 ]++ )
for ( loop[ 5 ] = 0; loop[ 5 ] <= 2; loop[ 5 ]++ )
for ( loop[ 6 ] = 0; loop[ 6 ] <= 2; loop[6]++ ) {
for ( int i = 6; i >= 0; i-- )
output.print(
letters[ digits[ i ] ][ loop[ i ] ] );
output.println();
}
input.setText( "Done" );
output.close(); // close output stream
} // end method actionPerformed
public static void main( String args[] )
{
Phone application = new Phone();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Every time i try to compile i get an error saying: Phone.java:96: error: reached end of file while parsing application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );^
1 error
i need help with either writing a new code or could someone please explain what i need to do to get my current code to work? thanks
Explanation / Answer
public class TelephoneNumberWordGenerator { 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'}}; Scanner input = new Scanner (System.in); private ObjectOutputStream output; PrintStream printStream; char[] word = new char [7]; public static void main(String[] args){ public void getPhoneNumber(){ System.out.println("Enter a seven-digit telephone number: "); phoneNumber = input.next(); } } 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; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.