CS 178 Programming with Multimedia Objects

Fall 2004

StringTokenizer Illustration and Recitation Exercise

Prepared by: Aditya Mathur.

To be used only for illustration in class on Nov 19, 2004.

Learning objectives:
  1. Using the StringTokenizer class.
  2. Sorting based on a specific field of an object.

Reference: Not in the book. You may refer to:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html#StringTokenizer(java.lang.String,%20java.lang.String)

Use StringTokenizer to separate tokens in a string. Tokens must be separated by a string known as a delimiter. For example, the string "I am Mr. Gotti" can be split into tokens "I", "am, "Mr.", and "Gotti" assuming the space as a delimiter. To separate a string into tokens, do the following:

1. Create a StringTokenizer object:

StringTokenizer st=new StringTokenizer(s, " "); // s is the string to be tokenized. The delimeter is a string containing

// only the space character. It could be any string in general.

2. Set up a loop to get the tokens.

while (st.hasMoreTokens()){ // hasMoreTokens() returns true if tokens remain, else it returns false.

String token=st.nextToken(); // nextToken() returns the next token in st.

// You may now do whatever you want with token.

}

If st="I am Mr. Gotti" then the variable token will take on values "I", "am", "Mr.", and "Gotti", in that order.

A more complex exercise to test your understanding of StringTokenizer will be available in the practice problems for the final exam.

Recitation exercise:

Given

final int maxToken=100;

String s=new String("");

String [] tokens=new String[maxTokens];

Write methods

  • getTokens() that extracts tokens in s and places them in tokens array. Assume that the tokens are separated by the space character.
  • sortByLength() sorts the tokens array in the ascending order of their lengths.
  • For example, if s="I love CS178 and Java" then getToken() should produce:
    • tokens={"I", "love", CS178", "and", "Java"}
    • and after sorting tokens={"I", "and", "love", "Java", "CS178"} or {"I", "and", "Java", "love", "CS178"}
Code (TokenizerIllustrator)

Last update: November 19, 2004