Question

Basically I need this while loop to perform 4 times

It will ask user for input then use that input in the nested if statements. (repeat 4 times)

The first prompt will show up and I am able to type an input.

but after that it just gives me an error.

and keeps pointing the error to "int userInput = scan.nextInt();"

I have no idea whats wrong with it. Can someone guide me? thanks

    int count = 0;

    while (count < 4 ) {

        System.out.println(prompt1);
        int userInput = scan.nextInt();

        (some nested if statements here to do my task, they all have
         count++ at the end)

    }
Was it helpful?

Solution

Always check if the nextInt is available before asking for it :

if(scan.hasNextInt())
{
 int userInput = scan.nextInt();
 // do something
}

OTHER TIPS

You need to define scan

Scanner scan = new Scanner(System.in);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top