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;
}