R
R
romangoward2019-10-27 03:38:12
Java
romangoward, 2019-10-27 03:38:12

How to reuse Scanner(System.in) if EOF arrives?

int var;

Scanner scanner = new Scanner(System.in);

while(scanner.hasNext() && scanner.hasNextLine()) {
    System.out.printf("Enter an integer: ");
    try {
        var = scanner.nextInt();
    } catch (InputMismatchException e) {
          scanner.nextLine();
    } catch (NoSuchElementException e) {
        // TODO: how to handle CTRL+D? 
    }
}

scanner.close();

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question