Using the StreamTokenizer class


This java code example shows how you can use the StreamTokenizer class to count words and numbers in a file.
In the example we open a stream to a file with help of the FileReader class.
We construct the StreamTokenizer object with this stream as argument and start looping through it.
The 'nextToken' method returns an int which indicates of what type the token is and we can match the int value to one of the static variables of the StreamTokenizer class to determine of what type the current token is.
The iterations continues until the end of file token is reached. For each token we check the tokenizers variable 'ttype', which holds the current token type, against the static variables to see if it is a number or a word and increment count variables accordingly.
So if the file content would look like this (without the quotes):

"5 ants is more than 4 elephants"

The output would be:

"Number of words in file: 5"
"Number of numbers in file: 2"


import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.StreamTokenizer;

/**
 *
 * @author javadb.com
 */

public class Main {
    
    /**
     * Example method for using the StreamTokenizer class
     */

    public void countWordsAndNumbers(String filename) {
        
        StreamTokenizer sTokenizer = null;
        int wordCount = 0, numberCount = 0;
        
        try {
            
            sTokenizer = new StreamTokenizer(new FileReader(filename));
            
            while (sTokenizer.nextToken() != StreamTokenizer.TT_EOF) {
                
                if (sTokenizer.ttype == StreamTokenizer.TT_WORD)
                    wordCount++;
                else if (sTokenizer.ttype == StreamTokenizer.TT_NUMBER)
                    numberCount++;
            }
            
            System.out.println("Number of words in file: " + wordCount);
            System.out.println("Number of numbers in file: " + numberCount);
            
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        new Main().countWordsAndNumbers("myFile.txt");
    }
}

Do you know your Java?
Take a Ten-Question-Java-Quiz!

Bookmark and Share




Need help with your Java code? It's secure and confidential.
This is how it works:
Send a detailed description of what you need help with, the more details the better. Also provide a deadline for when it has to be finished. More time means better chance of putting your request into the schedule.

If the request is serious you will shortly receive an email with the price, to which you have to respond if you accept.

Once you have accepted, the work will begin on developing your code by an experienced Java developer. When the code is finished a link to a secure payment will be sent to you.

The source code is then sent to you once the payment is completed.

IMPORTANT! The request needs to be very detailed, else it may be ignored.


Write your detailed request here:

E-mail address: