Java Reference
In-Depth Information
Tokens using a StringTokenizer:
This
is
a
test
which
is
simple
Tokens using the String.split() method:
This
is
a
test
which
is
simple
The StringTokenizer and the split() method of the String class return each token as a string. Sometimes you
may want to distinguish between tokens based on their types; your string may contain comments. You can have these
sophisticated features while breaking a character-based stream into tokens using the StreamTokenizer class.
Listing 7-44 illustrates how to use a StreamTokenizer class.
Listing 7-44. Reading Tokens from a Character-based Stream
// StreamTokenTest.java
package com.jdojo.io;
import java.io.StreamTokenizer;
import static java.io.StreamTokenizer.*;
import java.io.StringReader;
import java.io.IOException;
public class StreamTokenTest {
public static void main(String[] args) throws Exception{
String str = "This is a test, 200.89 which is simple 50";
StringReader sr = new StringReader(str);
StreamTokenizer st = new StreamTokenizer(sr);
try {
while (st.nextToken() != TT_EOF) {
switch (st.ttype) {
case TT_WORD: /* a word has been read */
System.out.println("String value: " +
st.sval);
break;
case TT_NUMBER: /* a number has been read */
System.out.println("Number value: " +
st.nval);
break;
}
}
 
Search WWH ::




Custom Search