0

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!

Community
  • 1
  • 1
  • Unless you want to assume VT100 codes, you don't have a lot of options. But why do it in the console anyway? Write a really tiny Swing app--problem solved. – Dave Newton Mar 27 '13 at 00:00

1 Answers1

1

I don't have answer to "clear stdin". I think that's OS specific, and that might not even worth trying.

However to solve your problem, you can use java.util.Timer that prompts the user at some random time. This will run in a separate thread. When your user finally pressed enter, check whether he/she has been prompted.

Code example below will print "Press enter" after 5 second. The main thread immediately blocks waiting for user input, if enter is pressed too early it will say so because the boolean switch isn't on yet

*Note: TimerTest is the name of the class I fiddle with. Feel free to change it to whatever your class name

static boolean userPrompted = false;

public static void main(String[] args) throws IOException {

    // Setup a 5 second timer that prompts the user and switch the userPrompted flag to true
    // This will run in a separate thread
    Timer timer = new Timer(false);
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            System.out.println("Press enter");
            synchronized(TimerTest.class) {
                userPrompted = true;
            }
        }
    }, 5000);

    // Blocks waiting for user input
    System.out.println("Get ready.. press enter as soon as you're prompted..");
    String input = new BufferedReader(new InputStreamReader(System.in)).readLine();

    // Check if user has been prompted
    synchronized (TimerTest.class) {
        if(!userPrompted) System.out.println("You pressed enter before prompted");
        else System.out.println("You pressed enter after prompted");
    }

}
gerrytan
  • 40,313
  • 9
  • 84
  • 99
  • Thanks for your answer. Yes that should do the work and I think I'll do that. Nevertheless, as you said, it's not a "clear terminal" answer so I'll wait for other options before validating your answer here. Thanks again. – FaustXVI Mar 27 '13 at 09:51