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:
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
|
|||||||||||
Code (TokenizerIllustrator) | |||||||||||
Last update: November 19, 2004 |
|||||||||||