Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable user input until Thread.sleep() finishes

I'm writing a really basic program, but I've run into a problem. The following is a slice of my code (it is a really silly program, don't try to guess what I'm using it for.)

System.out.println("Please press the Return button a 1000 times.");
    for(int i = 1;i < 25;i++) {
        input.nextLine();
    }
    System.out.println("Stop! Stop! Jeez, that was a joke! Do you think I'd make you press that button a 1000 times?");
    try {
    Thread.sleep(3000);
    } catch (InterruptedException e) {}
    System.out.println("Let's move on.");

What would happen here is that the program asks the user to press the Return button a 1000 times, which the user would eventually start spamming. The main problem is that after I declare that it was a joke and he only needed to press it 25 times, I'd like to disable the user input, as it's likely that the user will press the button multiple times before realizing that I was just joking. But when the thread.sleep is running, the user input is still active, which leads to multiple problems.

So, is there any way to disable the user input while the program is sleeping?

like image 610
Smodics Avatar asked Aug 13 '15 16:08

Smodics


1 Answers

You can control what to read from the console via application.. But to disable input entirely will be dependent on type of environment application is running on ... For E.g. in cmd line it should not allow you to type in after 25 enters... Whereas in IDE like eclipse, you can type in on console but it will not be read by application after 25 lines.

like image 53
Rohit sharma Avatar answered Oct 08 '22 22:10

Rohit sharma