forum

Home / DeveloperSection / Forums / Get java to wait for user input

Get java to wait for user input

Barbara Jones273719-May-2015

I’m attempt to make the main loop wait for the user to input something I added the loop:

while(!userInput.hasNext());

this is not work correctly. I have heard BufferedReader but I have never used it. So please 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);
    }

Updated on 19-May-2015

Can you answer this question?


Answer

1 Answers

Liked By