Name: paC48320 Date: 06/19/98
The StreamReader under JDK 1.1.5 no longer works
properly when eolIsSignificant(true) has been
called. Previous versions would return TT_EOL for
each newline encountered. In JDK 1.1.5 TT_EOL is
only returned for every second newline, or when
the next line is parsed. The first newline is
being absorbed either by the Reader (In my case
a BufferedReader and InputStreamReader) or by
the tokenizer itself. When parsing a file
this causes no problem, but when parsing
interactive input, this means that the user must
issue two EOL's for each line of input.
The following code will not respond to the first
line of input, and will only respond to each
subsequent line of input with the input from the
previous line:
import java.io.*;
class Test {
public static void main(String[] args) {
boolean running = true;
/* Create the tokenizer */
Reader r = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer tokenizer = new StreamTokenizer(r);
/* Enable eol checking */
tokenizer.eolIsSignificant(true);
while(running) {
try {
System.out.print("> ");
System.out.flush();
/* Read a line of input from the user */
args = ReadLine(tokenizer);
/* Process the users input */
System.out.println("Parsed input: " + args[0]);
} catch (IOException e) {
} /* Check for IO errors (try/catch IOException) */
} /* Loop as long as we are still running (while running) */
} /* main() */
private static String[] ReadLine(StreamTokenizer tokenizer)
throws IOException
{
int index = 0;
String[] retVal = new String[10];
read:
for (;;) {
switch(tokenizer.nextToken()) {
case StreamTokenizer.TT_EOF:
System.exit(0);
case StreamTokenizer.TT_EOL:
break read;
case StreamTokenizer.TT_WORD:
/* Add the token to the array */
retVal[index++] = tokenizer.sval;
break;
case StreamTokenizer.TT_NUMBER:
/* Add the token to the array */
retVal[index++] = Double.toString(tokenizer.nval);
break;
} /* See what kind of token we have (switch tokneizer.nextToken()) */
} /* Read until we get an EOL or EOF (for ;;) */
return(retVal);
} /* ReadLine() */
} /* class Test */
(Review ID: 28851)
======================================================================