String Tokenizer

Notes by Matt.

StringTokenizer class   from java.sun.com
  1. import java.util.StringTokenizer;  // (goes at the top of the program)
     
  2. StringTokenizer st = new StringTokenizer(String sToTokenize, String delimiter);  // (goes in method that uses the string tokenizer)
  3. StringTokenizer st = new StringTokenizer(String sToTokenize);  // (goes in method that uses the string tokenizer)
  4. A typical implementation after this is the following:

    while (st.hasMoreTokens()) {
      System.out.println(st.nextToken());
      System.out.println("Number of Tokens Left: " + st.countTokens());
    }


String Tokenizer Methods:

As shown above, the most common methods used on a String Tokenizer are:


Code Examples:

StringTok1.java (simple String Tokenizer, separate w/ spaces)

StringTok2.java (simple String Tokenizer, separate w/ colon)

StringTok3.java (using a String Tokenizer to read tokens from lines of a file) using input.txt