Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Java to wait for user Input

Tags:

java

I am trying to make an IRC bot for my channel. I would like the bot to be able to take commands from the console. In an attempt to make the main loop wait for the user to input something I added the loop:

while(!userInput.hasNext());

this did not seem to work. I have heard of BufferedReader but I have never used it and am not sure if this would be able to solve my problem.

while(true) {
        System.out.println("Ready for a new command sir.");
        Scanner userInput = new Scanner(System.in);

        while(!userInput.hasNext());

        String input = "";
        if (userInput.hasNext()) input = userInput.nextLine();

        System.out.println("input is '" + input + "'");

        if (!input.equals("")) {
            //main code
        }
        userInput.close();
        Thread.sleep(1000);
    }
like image 543
Ulsting Avatar asked May 14 '15 23:05

Ulsting


People also ask

Is there a way to wait in Java?

The easiest way to delay a java program is by using Thread. sleep() method. The sleep() method is present in the Thread class. It simply pauses the current thread to sleep for a specific time.

How do you create a wait method in Java?

wait() causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0). The current thread must own this object's monitor.

How do you take input until Enter is pressed in Java?

Using an Extra nextLine() to Wait for Enter Key in Java Scanner scans the text and parse primitive types like int and String . This class comes with a lot of methods that are used in input operations. The most commonly used methods are the nextInt() , nextLine , nextDouble , and etc.

How do you stop input in Java?

hasNextDouble() returns false using keyboard input, so that the last line of the code is executed, without changing the code? This code snippet calculates the average of the entered numbers. You want to send an EOF. See this question.


1 Answers

There is no need for you to check for available input waiting and sleeping until there is since Scanner.nextLine() will block until a line is available.

Have a look at this example I wrote to demonstrate it:

public class ScannerTest {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            while (true) {
                System.out.println("Please input a line");
                long then = System.currentTimeMillis();
                String line = scanner.nextLine();
                long now = System.currentTimeMillis();
                System.out.printf("Waited %.3fs for user input%n", (now - then) / 1000d);
                System.out.printf("User input was: %s%n", line);
            }
        } catch(IllegalStateException | NoSuchElementException e) {
            // System.in has been closed
            System.out.println("System.in was closed; exiting");
        }
    }
}

Please input a line
hello
Waited 1.892s for user input
User input was: hello
Please input a line
^D
System.in was closed; exiting

So all you have to do is to use Scanner.nextLine() and your app will wait until the user has entered a newline. You also don't want to define your Scanner inside the loop and close it since you're going to use it again in the next iteration:

Scanner userInput = new Scanner(System.in);
while(true) {
        System.out.println("Ready for a new command sir.");

        String input = userInput.nextLine();
        System.out.println("input is '" + input + "'");

        if (!input.isEmpty()) {
            // Handle input
        }
    }
}
like image 167
Raniz Avatar answered Sep 20 '22 13:09

Raniz