import java.io.*;
import java.util.*;  // to use the StringTokenizer class

/** A program that demonstrates the use of BufferedReader IO and
 the use of StringTokenizer to parse string tokens; the program
 reads each line from the keyboard, parses the string tokens of
 the line and output each, until an end-of-file indication is 
 reached (e.g. enter the character Ctrl-Z in Windows, or Ctrl-D 
 in Unix, in a line by itself) */ 

public class TestTokens {
  public static void main(String[] args) {

    String line;  // to hold the next input line
    StringTokenizer tokens;  // prepare to tokenize next line
    String token;  // hold the next token of line being processed
    int countTEST = 0;  // count occurrences of string TEST

    // open a character-based input stream (i.e., read from keyboard)
    BufferedReader in = new BufferedReader (
                 new InputStreamReader(System.in));
    // catch IOException when doing read, then ignore it
    try {
        int lineNumber = 1;
        // read until the end-of-file
        while ((line = in.readLine()) != null) { 
          // create a new StringTokenizer object
          tokens = new StringTokenizer(line);  
          System.out.println("Number of tokens of line " +
                  lineNumber + " = " + tokens.countTokens());
          while (tokens.hasMoreTokens()) {
            // extract the next token from the current line
            token = tokens.nextToken();  
            System.out.println("The next token = " + token);
            if (token.equals("TEST")) // look for string "TEST"
              countTEST++;
          }
          lineNumber++;
        }
    } catch (IOException e) {}
    System.out.println("Found the string TEST " + countTEST + " times.");
  }  // end of main()
}

