ShortWords.java

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

// A program to count the number of short words in
// any file (not just dictionary files)
//
// Jim Teresco, April 2002

public class ShortWords {

    
    public static void main(String[] args) {
        
        StreamTokenizer words;        // stream of words as tokens
        String curWord;                        // the current word
        
        int count = 0;                        // count of short words seen
        
        try {
            // create a stream tokenizer to access the file
            words = new StreamTokenizer(new FileReader(args[0]));

            // we reset the tokenizer to treat nothing as words, then
            // tell it things that contain characters 'A'...'z' should
            // be treated as words.  This also means things like this
            // (a // comment) will be included as words
            words.resetSyntax();
            words.wordChars('A','z');

            int nextTokenType;
            // get words until there are no more words
            while ( (nextTokenType=words.nextToken()) !=
                    StreamTokenizer.TT_EOF ) {
                // if the nextTokenType is TT_WORD, then we found 
                // a word (as opposed to some symbols or numbers)
                if (nextTokenType == StreamTokenizer.TT_WORD) {
                    // We access the actual word by a public instance
                    // variable (shame on those Java designers at Sun
                    // who did this)
                    curWord = words.sval;

                    // We want to remember it and count it if it
                    // had 4 letters in it
                    if (curWord.length() == 4 ) {
                        count++;
                        System.out.println(curWord);
                    }
                }
            }
                        
            System.out.println("Total = " + count );
            
        } catch ( IOException e) {
            System.out.println("Error reading dictionary file");
        }
    }
}