We just covered the try-catch topic in last night's lecture. It's not required to put it in this assignment, but I thought I would give it a shot.
I've been struggling with this for a while. Using a continue statement kicked it back to the start of the catch block so that the println inside it executed in a never ending loop.
So apparently there are these things called labels. I found that when I was trying to figure out how to solve the continue problem. I tried putting in a label just as I saw being done in the example code, and now it just gives me a compiler error on the break statement that says "the process flag is missing".
What am I doing wrong?
do {
// Prompt
try {
process: nbPlayers = keyboard.nextInt();
}
catch(Exception e) {
nbPlayers=0;
if (attempts<4) {
System.out.println("Incorrect input type. You have now made " + attempts +". Please enter an integer from 2 to 4.");
++attempts;
break process;
}
else {
System.out.println("Invalid input. You have now made " + attempts +". This game only allows 4 chances to input the requested number of players.\n Shutting down the program now.");
System.exit(0);
}
}
if(attempts < 4 && nbPlayers >= 2 && nbPlayers <= 4) {
valid = true;
} else if(attempts < 3) {
System.out.println("Invalid input. You have now made " + attempts +". This game only allows 4 chances to input the requested number of players.\nYou are allowed to try again.");
} else if(attempts == 3) {
System.out.println("Invalid input. You have now made " + attempts +". This game only allows 4 chances to input the requested number of players.\nYou are allowed to one more time.");
} else if(attempts == 4) {
System.out.println("Invalid input. You have now made " + attempts +". This game only allows 4 chances to input the requested number of players.\n Shutting down the program now.");
System.exit(0);
}
++attempts;
} while(!valid);