1
votes

My expectation:

If the user types an Int which is not in the right range, the program will give him another chance until the user gives the right type.

So, I need a while block. But I got an infinite loop.

My code:

import java.util.NoSuchElementException;
import java.util.Scanner;

public class TestInput {
    public static void main (String[] args) {
        boolean keepRunning = true;
        while (keepRunning) {
            try {
                System.out.println("Start --- ");
                Integer selection = inputSelection();
                //Integer selection = Integer.parseInt(selectionString);
                String email;
                switch (selection) {
                    case 1:
                        // inputDate();
                        System.out.println("Do 1");

                        break;
                    case 2:
                        //inputEmail();
                        System.out.println("Do 2");
                        break;
                    case 3:
                        //inputName();
                        System.out.println("Do 3");

                        break;
                    case 5:
                        // Exit
                        keepRunning = false;
                        break;
                    default:
                        System.out.println("Please enter a number between 1 and 3: ");
                        break;
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public static Integer inputSelection () {
        Integer selection = 0;
        try (Scanner scanner = new Scanner(System.in)) {
            if (scanner.hasNext()) {
                selection = scanner.nextInt();  // reads only one int. does not finish the line.
                scanner.nextLine(); // consume '\n' to finish the line
            }
        } catch (NoSuchElementException ex) {
            throw ex;
        }
        return selection;
    }
}

DOCs I read:

Resetting a .nextLine() Scanner

Using Scanner.nextLine() after Scanner.nextInt()

Scanner error with nextInt()

Scanner is skipping nextLine() after using next() or nextFoo()?

How to use java.util.Scanner to correctly read user input from System.in and act on it?

The Scanner Class String formatting in Java Scanner class Java Scanner doesn't wait for user input

Java String Scanner input does not wait for info, moves directly to next statement. How to wait for info?

String formatting in Java Scanner class

Scanner.html#nextInt()

Those answers do not work for me. I still get an infinite loop. Any advice? Thank you.

2
Have you tried throwing away the next input by using scanner.next()? - cs1349459

2 Answers

0
votes

try to set keepRunning on false, if it so happens, that you reach in to a case state.

0
votes

In Java, the break statement breaks out of the current iteration to the next enclosing level of control. It seems you expect the break in the switch to break you out of the while loop, but it only gets you out of the current case.

Try changing the keepRunning flag within any of the cases representing a success condition, rather than the failure condition. keepRunning should be changed to false when you get good input, not bad input. You only want to continue asking when you get bad input, correct?