Clearning Input Stream in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Buddybot111
    New Member
    • Apr 2008
    • 1

    #1

    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.read Line() ) 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.readL ine() 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.readL ine() 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.readL ine(). 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
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by Buddybot111
    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.read Line() ) 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.readL ine() 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.readL ine() 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.readL ine(). 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
    You can create a class that implements Runnable....

    Override the run method

    [CODE=java]@Override public void run(){
    try{
    Thread.sleep(lo ng milliseconds);
    //Prompt the user for input here......
    }catch(Interrup tedException e){}
    }[/CODE]

    Executing this class will create a new Thread that runs concurrently on the original thread....

    Don't attempt to Exit the program after starting this thread.....
    Try to have a loop ( do while loop ) to run the program continuously... .

    Have some experiment on it....

    regards,
    sukatoa

    Comment

    Working...