ShortWords.java

import java.awt.*;
import java.io.*;
import java.awt.event.*;

// A program to count the number of short words in a 
// dictionary file
public class ShortWords {

    
    public static void main(String[] args) {
        TextArea wordList;
        FileDialog dialog;                // lets user select file name
        String fileName;                // the file name chosen
        
        BufferedReader words;        // stream of dictionary words
        String curWord;                        // the current word
        String fourLetters = new String();
        
        Frame frame = new Frame();
        wordList= new TextArea( 30, 20 );
        frame.add(wordList, "Center");
        frame.show();
        
        int count = 0;                        // count of short words seen
        
        try {
            // create and display dialog box to get file name
            dialog = new FileDialog( new Frame(), 
                                     "Select dictionary file",
                                     FileDialog.LOAD);
            dialog.show();
            fileName = dialog.getDirectory() + dialog.getFile();
            
            // create a stream to access dictionary file
            words = new BufferedReader(new FileReader(fileName));
                                
            // get the first line of the file
            curWord = words.readLine();
            while ( curWord != null ) {
                if ( curWord.length() == 4 ) {
                    count++;
                    fourLetters = fourLetters + curWord + "\n";
                }
                
                // get the next line from the file
                curWord = words.readLine();
            }
                        
            wordList.append( fourLetters );
            wordList.append( "Total = " + count );
            
        } catch ( IOException e) {
            System.out.println("Error reading dictionary file");
        }
    }
}