Disable user input until Thread.sleep () finishes

I am writing a really basic program, but I have a problem. The following is a snippet of my code (this is a really stupid program, do not try to guess what I use 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 move on."); 

What will be here is that the program asks the user to press the return button 1000 times, which ultimately the user will start spamming. The main problem is that after I declare that it was a joke, and he only needed to press 25 times, I would like to disable user input, since most likely the user will press the button several times before realize that I'm just a joke. But when thread.sleep is executed, user input is still active, which leads to a lot of problems.

So, is there a way to disable user input during sleep?

+6
source share
3 answers

You can control what to read from the console using the application. But to disable input, it completely depends on the type of application of the environment on which it is running ... For example, in the cmd line it should not allow you to enter after 25 inputs ... While in the IDE, for example eclipse, you can enter the console but will not be considered an application after 25 lines.

+1
source

Look at this:

 public class Main { private static final String QUIT = "quit"; private static final int COUNT = 1000; public static void main(String[] args) throws InterruptedException { new Main(new BufferedReader(new InputStreamReader(System.in))).main(); } private final BufferedReader in; private final BlockingQueue<String> lines = new ArrayBlockingQueue<>(1); private volatile boolean ignore; public Main(BufferedReader in) {this.in = in;} private void main() throws InterruptedException { Thread inputReader = new Thread(this::read, "input-reader"); inputReader.setDaemon(true); System.out.println("Please press the Return button a "+COUNT+" times."); inputReader.start(); String line = ""; for(int i = 1;i <= 25;i++) { line = lines.take(); System.out.println("Good! You entered '"+line+"'. Only "+(COUNT-i)+" left."); } System.out.println("Stop! Stop! Jeez, that was a joke! Do you think I'd make you press that button a "+COUNT+" times?"); ignore = true; Thread.sleep(3000); ignore = false; String optionalLine = lines.poll(); if(optionalLine!=null) { line = optionalLine; System.out.println("Ignored:" + line); } System.out.println("Let move on. Type "+QUIT+" when you're tired."); while(!QUIT.equalsIgnoreCase(line)){ line = lines.take(); System.out.println(">"+line); } System.out.println("Bye."); } private void read(){ try { String line = in.readLine(); while(line!=null){ if (ignore) { boolean inserted = lines.offer(line); if (!inserted) System.out.println("Ignored:" + line); } else {lines.put(line);} line = in.readLine(); } } catch (IOException|InterruptedException e) {e.printStackTrace();} } } 
0
source

I believe that adding 1 line to your code is enough (a big james suggestion):

 System.out.println("Let move on."); System.in.read(new byte[System.in.available()]);//read and ignore 
-1
source

All Articles