---
title: "Get java to wait for user input"  
description: "Get java to wait for user input"  
author: "Barbara Jones"  
published: 2015-05-19  
updated: 2015-05-19  
canonical: https://www.mindstick.com/forum/23241/get-java-to-wait-for-user-input  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Get java to wait for user input

I’m attempt to make the main loop wait for the [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) to [input](https://www.mindstick.com/forum/159209/how-can-i-read-convert-an-input-stream-into-a-string-in-java) something I added the loop:

while(!userInput.hasNext());

this is not work correctly. I have heard [BufferedReader](https://www.mindstick.com/blog/11121/java-i-o-character-streams-and-buffered-reader-writers) but I have [never](https://yourviews.mindstick.com/view/290/zoos-prisons-for-beings-who-have-never-committed-a-crime) 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);    }
```

## Replies

### Reply by Lillian Martin

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        }    }   
}
```


---

Original Source: https://www.mindstick.com/forum/23241/get-java-to-wait-for-user-input

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
