How to clear the standard input (term) in Java?
A little bit of history : I'm writing a "reflex" program, the algorithm is very simple :
wait a random amount of time
print "press enter"
read line
The point is, if the user pressed by mistake the enter key, it will be read so the test will be wrong. My goal is to correct that bug. To do so, I want to have an algorithm like this :
wait a random amount of time
clear stdin
print "press enter"
read line
But I can't find a way to do that. This post seems to be interesting : available
gets the number of remaining chars and skip
will skip them. Working only on paper. If you stress the application by pressing multiple times the enter key, the available
method returns
an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking
so, sometime, the return value is wrong an at least one carriage return remains in the buffer and the bug is still here.
This solution may do the trick but it's platform dependent which I don't want to be. Furthermore, calling a system routine from Java is quite a bad idea.
To resume, I want to clear the standard input of my program but I don't want to close it nor block my program waiting for the user to give an input. It seems a very basic problem to me so shame on me if the answer is obvious!