Question

So, for an assignment for class, I have to take user input and assign the amount of "food" that my gerbil objects can use per day. In this situation, I have already taken the max amount of food daily from the user and need to give an error message to the user if they attempt to input a value above the daily max.

If this is the case, they need to be re-prompted to enter the amount of food the gerbil eats.

I can't seem to figure out how to break out of the "if" statement and go back to the top of the "for" loop. Here is my code:

for (int j = 0; j < numberOfFoods; j++) {
    System.out.println(gerbilId[index].substring(index)
            + " eats how many " + foodNames[j] + "s per day");
    int amountOfFood = keyboard.nextInt();

    if (amountOfFood > foodMax[j]) {
        System.out.println("Error. Please input a valid amount of food");
        break;
    } else {
        gerbilConsumption[index] = amountOfFood;
    }
}
Was it helpful?

Solution

The answers saying use continue are wrong as they are not what you're looking for in this situation. continue will move you on to the next food type, but if the input was invalid you want to stay on the same one. You need to do j--; to make that number be used again

if (amountOfFood > foodMax[j]) {
    System.out.println("Error. Please input a valid amount of food");
    j--;
} else {
    gerbilConsumption[index] = amountOfFood;
}

OTHER TIPS

If you want to restart the for loop then do this:

for (int j = 0; j < numberOfFoods; j++) {
    System.out.println(gerbilId[index].substring(index)
            + " eats how many " + foodNames[j] + "s per day");
    int amountOfFood = keyboard.nextInt();

    if (amountOfFood > foodMax[j]) {
        System.out.println("Error. Please input a valid amount of food");
        j = -1;  //instead of break
    } else {
        gerbilConsumption[index] = amountOfFood;
    }
}

The variable j will become 0 in the next iteration and the loop restarts.

On the other hand, if you only want to continue the for loop then use continue in place of break. But this is pointless in this case as that will happen automatically.

As it's unclear what is needed here, if you want to stay in the same iteration level, I'd advise you use a while loop inside.

for (int j = 0; j < numberOfFoods; j++) {
    System.out.println(gerbilId[index].substring(index)
            + " eats how many " + foodNames[j] + "s per day");
    int amountOfFood = keyboard.nextInt();

    while (amountOfFood > foodMax[j]) {
        System.out.println("Error. Please input a valid amount of food");
        amountOfFood = keyboard.nextInt();
    }
    gerbilConsumption[index] = amountOfFood;
}

You could also re-structure to use a do...while loop -

for (int j = 0; j < numberOfFoods; j++) {
    System.out.println(gerbilId[index].substring(index)
            + " eats how many " + foodNames[j] + "s per day");
    int amountOfFood=-1;

    do 
    {
       amountOfFood = keyboard.nextInt();
       if (amountOfFood > foodMax[j] || amountOfFood <0) {
        System.out.println("Error. Please input a valid amount of food");
        amountOfFood=-1;
       }
    } while (amountOfFood == -1);

    gerbilConsumption[index] = amountOfFood; 
}

give an error message to the user if they attempt to input a value above the daily max.

If this is the case, they need to be re-prompted to enter the amount of food the gerbil eats.

You can't skip out of your for loop because you are adding to J which will confuse you.

Use code like this if you need to accomplish the above(insert it into your for loop)

while(true){
//prompt amount of gerbil food
//if input equal or below daily max then BREAK;
//tell user they are above daily max
}

Or using your code from your post...

int amountOfFood;
while(true){
 amountOfFood=keyboard.nextInt();
 if (amountOfFood <= foodMax[j]) break;
 System.out.println("Error. Please input a valid amount of food");
}
 gerbilConsumption[index] = amountOfFood;

As you can see I inverted your check. It is better for you to check what IS valid, rather than check what IS NOT valid.

Technically, you are looking for the Java key word continue.

The keyword break will break out of the for loop altogether.

The keyword continue will skip the rest of that iteration of the for loop and start the next iteration of it.

For example:

//loops 3 times
for(int i = 0; i < 3; i++) {
    if(i == 1) {
        continue;
    }
    System.out.println("Iteration #" + i);
}

Will print:

Iteration #0
Iteration #2

This answers the spirit of your question.

However, as others have pointed out, continue is not actually what you want for your particular example. What you really want to use is just j--. But for future reference, you now know how to break out of an "if" statement and go back to the top of a for loop.

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