Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-25-2008, 02:10 PM   PM User | #1
Buddybot111
New to the CF scene

 
Join Date: Mar 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Buddybot111 is an unknown quantity at this point
Clearning Input Stream in Java

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.

Please help,
Michael
Buddybot111 is offline   Reply With Quote
Old 04-26-2008, 01:27 PM   PM User | #2
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
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
__________________
You never have to change anything you got up in the middle of the night to write. -- Saul Bellow
shyam is offline   Reply With Quote
Old 04-26-2008, 02:54 PM   PM User | #3
Buddybot111
New to the CF scene

 
Join Date: Mar 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Buddybot111 is an unknown quantity at this point
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.
Buddybot111 is offline   Reply With Quote
Old 04-27-2008, 08:56 PM   PM User | #4
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
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 )
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch
Aradon is offline   Reply With Quote
Old 04-27-2008, 09:37 PM   PM User | #5
Buddybot111
New to the CF scene

 
Join Date: Mar 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Buddybot111 is an unknown quantity at this point
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.
Buddybot111 is offline   Reply With Quote
Old 04-28-2008, 04:33 AM   PM User | #6
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
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:

InputStream
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch

Last edited by Aradon; 04-28-2008 at 04:35 AM..
Aradon is offline   Reply With Quote
Old 04-29-2008, 12:52 PM   PM User | #7
Buddybot111
New to the CF scene

 
Join Date: Mar 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Buddybot111 is an unknown quantity at this point
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.
Buddybot111 is offline   Reply With Quote
Old 04-29-2008, 08:12 PM   PM User | #8
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
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.
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch
Aradon is offline   Reply With Quote
Old 04-29-2008, 11:40 PM   PM User | #9
Buddybot111
New to the CF scene

 
Join Date: Mar 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Buddybot111 is an unknown quantity at this point
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.
Buddybot111 is offline   Reply With Quote
Old 05-01-2008, 01:39 AM   PM User | #10
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
To continue looking at the inputStream, can you use a bufferedInputStream?

BufferedInputStream

That SHOULD work, apparently the InputStream available always returns 0, while this one will say the bytes available in the stream.
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch
Aradon is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:20 PM.


Advertisement
Log in to turn off these ads.