Sorry, this will be a long post, perhaps...
I have run into a few strange issues with using loops and System.in.read(). I am getting multiple passes on loops that should only get one pass before any input (or at least they sure look like they should only be getting one pass to me...).
I will give three of my code examples and maybe someone can tell me why I'm getting such strange output.
first, this example, a simple for loop that is going to loop and ask for input until the correct character (in this case, "S") is entered:
OK, I would be expecting output something along these lines...
But instead I get this!!!
If I press ENTER it loops twice, not once, before prompting me again. If I enter a wrong character, it loops thrice! Why is this? How can I prevent it?
Here is another example of odd output in a somewhat similar program.
OK, this one does a very similar thing. Three loops when I press a character, two loops if I press enter.
Now something that confuses me a little more, but also makes me wonder if it is line buffered input is this program, which works just fine:
OK, this one gives me PERFECTLY normal output. Only one prompt and one pass per entry. Nothing weird here. In this one I added an exclusion for line feeds and carriage returns. I suspect these are the culprits in AnnoyingForProg and AnnoyingDWhileProg, but I am not 100% certain. Could some Java guru give me a hand here and explain what's going on?
Thanks a million, and again, sorry for the excessively long post.
-Ender
[This message has been edited by Ender Everett (edited June 01, 2001).]
I have run into a few strange issues with using loops and System.in.read(). I am getting multiple passes on loops that should only get one pass before any input (or at least they sure look like they should only be getting one pass to me...).
I will give three of my code examples and maybe someone can tell me why I'm getting such strange output.
first, this example, a simple for loop that is going to loop and ask for input until the correct character (in this case, "S") is entered:
OK, I would be expecting output something along these lines...
But instead I get this!!!
If I press ENTER it loops twice, not once, before prompting me again. If I enter a wrong character, it loops thrice! Why is this? How can I prevent it?
Here is another example of odd output in a somewhat similar program.
OK, this one does a very similar thing. Three loops when I press a character, two loops if I press enter.
Now something that confuses me a little more, but also makes me wonder if it is line buffered input is this program, which works just fine:
OK, this one gives me PERFECTLY normal output. Only one prompt and one pass per entry. Nothing weird here. In this one I added an exclusion for line feeds and carriage returns. I suspect these are the culprits in AnnoyingForProg and AnnoyingDWhileProg, but I am not 100% certain. Could some Java guru give me a hand here and explain what's going on?
Thanks a million, and again, sorry for the excessively long post.
-Ender
[This message has been edited by Ender Everett (edited June 01, 2001).]