java wait for user input

Solutions on MaxInterview for java wait for user input by the best coders in the world

showing results for - "java wait for user input"
Jade
05 Aug 2018
1public class ScannerTest {
2
3    public static void main(String[] args) {
4        Scanner scanner = new Scanner(System.in);
5        try {
6            while (true) {
7                System.out.println("Please input a line");
8                long then = System.currentTimeMillis();
9                String line = scanner.nextLine();
10                long now = System.currentTimeMillis();
11                System.out.printf("Waited %.3fs for user input%n", (now - then) / 1000d);
12                System.out.printf("User input was: %s%n", line);
13            }
14        } catch(IllegalStateException | NoSuchElementException e) {
15            // System.in has been closed
16            System.out.println("System.in was closed; exiting");
17        }
18    }
19}
20