Re: Does StreamTokenizer.pushBack correctly update TT_EOF ?
In article
<d855d5e7-449a-4f35-b6b4-14384ddce84a@r15g2000prh.googlegroups.com>,
enokacorea@gmail.com wrote:
[...]
My issue is that I thought my code should give the identical output
either if a) I insert a readToken() immediately followed by a
pushBack() or b) with neither of these statements (all else being the
same). I find that this is not the case.
I am unable to reproduce what you describe:
<code>
import java.io.*;
public class Tokenizer {
public static void main(String[] args) throws IOException {
StreamTokenizer tokens = new
StreamTokenizer(new StringReader("Test 123"));
int token;
token = tokens.nextToken();
print(tokens, token);
token = tokens.nextToken();
print(tokens, token);
tokens.pushBack();
tokens.pushBack();
tokens.pushBack();
token = tokens.nextToken();
tokens.pushBack();
token = tokens.nextToken();
tokens.pushBack();
token = tokens.nextToken();
print(tokens, token);
token = tokens.nextToken();
print(tokens, token);
}
private static void print(StreamTokenizer tokens, int token) {
System.out.println((token == StreamTokenizer.TT_EOF)
+ " " + tokens.sval + " " + tokens.nval);
}
}
</code>
<console>
false Test 0.0
false null 123.0
false null 123.0
true null 123.0
</console>
After further debugging I removed
while (sis.ttype != StreamTokenizer.TT_EOF) and put in a
while (!EOF)
where EOF is set if (sis.ttype == StreamTokenizer.TT_EOF) immediately
after the "chief" nextToken() - which is not subject to pushBack().
This solved the problem !!
Excellent! I would also develop tests to ensure that your code rejects
invalid input.
By the way, I visited your web site and was intrigued to find that you
are a doctor of medicine in addition to being a computer expert !
I'm no expert, but sometimes I apply software engineering principles to
medical informatics. I rarely appear on TV. :-)
--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews