Home > Back-end >  Java Scanner.nextInt() gets skipped?
Java Scanner.nextInt() gets skipped?

Time:10-13

I wrote some code that needs an int variable to hold a value between 0 and 5, and I wrote a while loop to validate that the user inputs a valid value (0 to 5), to do so, like this:

import java.util.Scanner;
public class ZeroToFive {
public static void main(String[] args) {
    //some code...
    Scanner scanner = new Scanner (System.in);
    int someNumber = -1;  
    while(someNumber < 0 || someNumber > 5)
            System.out.println("Enter a number from 0 to 5");
            someNumber=scanner.nextInt();
            if(someNumber < 0 || someNumber > 5){
                System.out.println("Invalid data. You must enter a number from 0 to 5. Try again.");
            }
     // some code that needs someNumber to be a number from 0 to 5...       
    } 
}

The plan is to update the someNumber variable with the user input, but the loop keeps running without stopping to read the nextInt().

Output: Enter a number from 0 to 5 Enter a number from 0 to 5 Enter a number from 0 to 5 Enter a number from 0 to 5 Enter a number from 0 to 5 ... (forever)

Why the loop does not stop to read the nextInt() from the user?

CodePudding user response:

The problem is because of the missing {...} for the while loop.

It should be

while (someNumber < 0 || someNumber > 5) {
    System.out.print("Enter a number from 0 to 5: ");
    someNumber = scanner.nextInt();
    if (someNumber < 0 || someNumber > 5) {
        System.out.println("Invalid data. You must enter a number from 0 to 5. Try again.");
    }
    // some code that needs someNumber to be a number from 0 to 5... 
}

Because of the missing bracket, only the following statement

System.out.println("Enter a number from 0 to 5");

is in the scope of the while loop.

CodePudding user response:

So, basically the condition while(someNumber < 0 || someNumber > 5) is always true because you initiated the variable "someNumber" with the value -1. What you wanna do is contain both the System.out.println("Enter a number from 0 to 5"); and the someNumber=scanner.nextInt(); in the while instruction using semicolons in order to let the someNumber variable change its value.

The code should look like:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        //some code...
        Scanner scanner = new Scanner (System.in);
        int someNumber = -1;
        while(someNumber < 0 || someNumber > 100){
            System.out.println("Enter a number from 0 to 5");
            someNumber=scanner.nextInt();
        }
        if(someNumber < 0 || someNumber > 5){
            System.out.println("Invalid data. You must enter a number from 0 to 5. Try again.");
        }
        // some code that needs someNumber to be a number from 0 to 5...
    }
}

CodePudding user response:

If you do not specify {} after the loops only the first following line of code will be executed (in this case System.out.println ("Enter a number from 0 to 5")). To execute block of code you need to specify {} after while(someNumber < 0 || someNumber > 5)

  • Related