Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Mar 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    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

  • #3
    New to the CF scene
    Join Date
    Mar 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    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

  • #5
    New to the CF scene
    Join Date
    Mar 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    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
    Last edited by Aradon; 04-28-2008 at 04:35 AM.
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #7
    New to the CF scene
    Join Date
    Mar 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    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

  • #9
    New to the CF scene
    Join Date
    Mar 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #10
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •