THE PACKAGE JAVA.IO
java.io.StreamTokenizer
22.14
Here is a simple example of the use of a
StreamTokenizer
. The following
code merely reads all the tokens in the standard input stream and prints an identi
fication of each one. Changes in the line number are also noted.
import java.io.StreamTokenizer;
import java.io.IOException;
class Tok {
public static void main(String[] args) {
StreamTokenizer st = new StreamTokenizer(System.in);
st.ordinaryChar( / );
int lineNum = 1;
try {
for (int tokenType = st.nextToken();
tokenType != StreamTokenizer.TT_EOF;
tokenType = st.nextToken()) {
int newLineNum = st.lineno();
if (newLineNum != lineNum) {
System.out.println("[line " + newLineNum
+ "]");
lineNum = newLineNum;
}
switch(tokenType) {
case StreamTokenizer.TT_NUMBER:
System.out.println("the number " + st.nval);
break;
case StreamTokenizer.TT_WORD:
System.out.println("identifier " + st.sval);
break;
default:
System.out.println(" operator "
+ (char)tokenType);
}
}
} catch (IOException e) {
System.out.println("I/O failure");
}
}
}
If the input stream contains this data:
10 LET A = 4.5
20 LET B = A*A
30 PRINT A, B
then the resulting output is:
[line 1]
the number 10.0
identifier LET
713
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved