Users Pricing

forum

home / developersection / forums / get java to wait for user input

Get java to wait for user input

Barbara Jones 3207 19 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);
    }

1 Answers