Softy
Softy

Reputation: 5

Keep asking for user input until an integer is between two numbers

I need help with the following code:

    int seatNum = 0;

    Scanner seatNumber = new Scanner(System.in);

    do
    {
        try 
        {
            System.out.println("Please choose a seat number");
            seatNum = seatNumber.nextInt();  
        } 
        catch(InputMismatchException e) 
        {
            System.out.println("The index you have entered is invalid");
            System.out.println("Please enter an index number between 0 and 7");

        }

    } while (seatNum <= 0 || seatNum >= 7);

Basically, all I want is for seatNum to be between 0 and 7 if not ask the user to input again. When I input an integer that's not between 0 and 7 everything works correctly but if I enter a String the program goes to an infinite loop. How do I stop the program of going into an infinite loop when the user inputs String?

Upvotes: 0

Views: 2261

Answers (4)

worldlife83
worldlife83

Reputation: 21

Either you can use BufferedReader or use Scanner with an additional seatNumber.nextLine() with above explanation given by @lazy.coder

    int seatNum = 0;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    do {
        try {
            System.out.println("Please choose a seat number");
            //   seatNum = seatNumber.nextInt();
            seatNum = Integer.valueOf(br.readLine());
        }
        catch (InputMismatchException | IOException | NumberFormatException nf) {
            System.out.println("The index you have entered is invalid");
            System.out.println("Please enter an index number between 0 and 7");
        }
    } while (seatNum <= 0 || seatNum >= 7);
}

Upvotes: 2

Shamshirsaz.Navid
Shamshirsaz.Navid

Reputation: 2362

You could change it like this:

        int seatNum = 0;
        Scanner seatNumber = new Scanner(System.in);
        do
        {
            try
            {
                System.out.println("Please choose a seat number");
                try{
                    seatNum = Integer.parseInt(seatNumber.next());
                }catch (Exception ignored){}
            }
            catch(InputMismatchException e)
            {
                System.out.println("The index you have entered is invalid");
                System.out.println("Please enter an index number between 0 and 7");

            }

        } while (seatNum <= 0 || seatNum >= 7);

Upvotes: 0

Abhinav Chauhan
Abhinav Chauhan

Reputation: 1384

Try this

String seatNum = null;
Scanner scanner = new Scanner(System.in);
while(true){
System.out.println(" Enter a number between 0 and 7"); 
seatNum = scanner.nextLine().toString();
if(seatNum.matches("[0-7]")) break; // makes sure user enter a number also between the range
else System.out.println("Enter between 0 and 7");

Nice right ? i din't run it, tell if it is what you want

Upvotes: 0

lazy.coder
lazy.coder

Reputation: 461

    int seatNum = 0;
    Scanner seatNumber = new Scanner(System.in);
    do {
        try {
            System.out.println("Please choose a seat number");
            seatNum = seatNumber.nextInt();
        }
        catch(InputMismatchException e) {
            System.out.println("The index you have entered is invalid");
            System.out.println("Please enter an index number between 0 and 7");
            seatNumber.nextLine();
        }
    } while ( (seatNum <= 0 || seatNum >= 7) );

add seatNumber.nextLine(); in the catch block. This command advances the scanner to the next line (when reading from a file or string, this simply reads the next line), thus essentially flushing it, in this case. It clears the buffer and readies the scanner for a new input.

Upvotes: 1

Related Questions