import java.io.*;
import java.util.*;

/**
   A counter used to count number of words, characters, and lines from
   a given text.
   @author Sugiharto Widjaja
   @version 08/21/02
*/
public class WordCounter
{
   /**
      Consruct the word counter
      @param text the given text
   */
   public WordCounter(String text)
   {
      this.text = text;
      numOfWords = 0;
      numOfChars = 0;
      numOfLines = 0;
   }

   /**
      Count the number of words and characters
   */
   public void countWord()
   {
      StringTokenizer tokenizer = new StringTokenizer(text, " \n");
      while(tokenizer.hasMoreTokens())
      {
         numOfWords++;
         String token = tokenizer.nextToken();
         numOfChars += token.length();
      }
   }

   /**
      Count the number of lines
   */
   public void countLine()
   {
      StringTokenizer tokenizer = new StringTokenizer(text, "\n");
      while(tokenizer.hasMoreTokens())
      {
         String token = tokenizer.nextToken();
         numOfLines++;
      }
   }

   /**
      Return the number of words
      @return number of words
   */
   public int getNumOfWords()
   {
      return numOfWords;
   }

   /**
      Return the number of characters
      @return number of characters
   */
   public int getNumOfChars()
   {
      return numOfChars;
   }

   /**
      Return the number of lines
      @return number of lines
   */
   public int getNumOfLines()
   {
      return numOfLines;
   }

   // The given text
   private String text;
   // Number of words
   private int numOfWords;
   // Number of characters
   private int numOfChars;
   // Number of lines
   private int numOfLines;
}
