Search code examples
javalabeltry-catchbreakcontinue

Using try, catch and a label for the first time


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

Solution

  • Your problem is not quite clear. Let's talk on a code. What do you think of this ?

     int attempts = 0; // is it a good initialization ?
     int nbPlayers = 0; // is it a good initialization ?
     boolean valid = false; // is it a good initialization ?
     String nbPlayersString = "";
     do {
            // Prompt
            
        try {
               nbPlayersString = keyboard.nextLine(); 
               nbPlayers = Integer.parseInt(nbPlayersString);
               if(2<= nbPlayers  && nbPlayers <= 4) {
                   valid = true;
               }
               else {
                   System.out.println("Bad Attempt " + (attempts + 1) +". Invalid number of players.");
               }
            }
            catch(Exception e) {
                nbPlayers=0; 
                System.out.println("Bad Attempt " + (attempts + 1) +". Wrong input type. ");
      
            }
        
            
    
            ++attempts;
            
        } while(!valid && attempts < 4);
     
     if(valid)
         System.out.println("GOOD JOB!");
     else {
         System.out.println("You have exhausted all your chances. Program will terminate!");
         System.exit(0);
     }
    

    Final EDIT It looks like you also had a cache problem with Scanner.nextInt() after entering an invalid value (string instead of integer). So, in the attempts following, the Scanner cache still had the bad value and considered it.