View Full Version : Clearning Input Stream in Java
04-25-2008, 02:10 PM
I don't have the code currently on me (at home right now, and code is on school computers), but I should be able to explain my problem.
I'm using the SavitchIn class to read input from the user. I have code setup so that it asks the user to press enter (using SavtitchIn.readLine() ) and then proceeds to run a loop that basically pauses the system for 5 seconds after the user presses enter, after the 5 seconds the word "BEGIN!" flashes onto the console and with SavitchIn.readLine() called again the user begins typing a specified sentence. The object of the program is to see how fast the user can type the sentence. Now my problem is that for some reason, during the loop that pauses the system for 5 seconds the user is able to type and have whatever they typed suddenly show up as soon as the SavitchIn.readLine() is called after "BEGIN!". In effect, the user can cheat the game and type the entire sentence and just hit enter before Begin! even flashes and this will all be saved and read in the next calling of SavitchIn.readLine(). How do I fix this? I figure there must be some way to clear the input stream, but attempts of google searching have just left me frustrated.
04-26-2008, 01:27 PM
have you considered using a flag to signify when it is actually acceptable to read input from the user and ignoring anything that is entered before that
04-26-2008, 02:54 PM
I'm not exactly sure what you mean. As far as I can see, you shouldn't be able to input anything in the first place, after all the function that allows you to isn't executed untill after "BEGIN!". And the function before the pause timer should close the input stream according to the SavitchIn class after the user presses enter (which they must for the program to go on). I've tried possible ways of viewing the input stream to see if they typed anything before they were supposed to, but nothing I could come up with would allow me to view what they typed without calling SavitchIn.readLine() which basicaly defeats the purpose.
04-27-2008, 08:56 PM
Why don't you just wait the five seconds, call the readline and then start the timer?
So after you sleep your thread for five seconds, do an initial readLine to clear the input stream, and then you can continue with the user inputting whatever they need to input.
Since the input stream should be cleared right away there shouldn't be any difference between them starting and it getting trapped in that readLine.
(also this should be moved to the Java forum ;) )
04-27-2008, 09:37 PM
If I get what your saying: you want me to add an additional readLine() after the 5 second wait, but before the timer starts and before the readLine() that is after the timer is called. I've tried this and it fails for two reasons. One: simply the user is forced hit enter again unneccessarily to go onto the actuall typing part. And two: the user can still cheat the program by hitting enter during the 5 second wait (which bypasses that added readLine() statement) and then start typing the setence the same way they would have circumvented the program before.
04-28-2008, 04:33 AM
Hm, true. I'm unsure of a particular way to tell if there is data inside the input buffer except maybe through the InputStream of System.in.
That is, check to see if System.in.available() returns some positive number of bytes, if it does, then something is in the input stream and you should read a Line, otherwise you are fine. (I've never done this nor do I know if the behavior is really like this, but reading the API it sounds like it)
Another method could be a simple check. We can assume that no person can write an entire word under .001 miliseconds (or whatever threashold you want to use) and so if you get input during that threshold you know that the user is cheating the system and you can just reopen the stream and allow the user to enter data then.
(This is the assumption that your readLine uses System.in. Since you are using a custom class, it is hard to tell.)
EDIT: As an additional thought, if you are using a pure InputStream you could possibly mark the beginning of the stream when you open it and then reset to that mark when the timer is up. This might work, but once again I haven't tried it. All API possibilities here:
04-29-2008, 12:52 PM
I tried all of your suggestions, none of them worked :( This is driving me crazy! SavitchIn does use the InputStream class, but I have no way of reading the input stream untill the user presses enter so seeing if they typed something too fast is fairly useless considering I can't even see if they typed anything untill they want me to see it.
04-29-2008, 08:12 PM
I still think that my simple check method might also work as well. Just create some imaginary bounds, so if the timer is at some amount of milliseconds or below, we know it's a cheater and so just ignore the input and wait for the person to hit more input and enter again.
Of course you could then restart the timer in order to catch any errant enter keys after that.
04-29-2008, 11:40 PM
I understand the logic behind the check method, but there is no way I can figure out to see what the user has typed before they actually hit ENTER. Meaning if they type half the sentence in what the timer perceives as a millisecond, it doesn't matter because there still not hitting enter untill afterwards so I have no way of seeing that they typed that first half before the timer started.
05-01-2008, 01:39 AM
To continue looking at the inputStream, can you use a bufferedInputStream?
That SHOULD work, apparently the InputStream available always returns 0, while this one will say the bytes available in the stream.
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.