-3

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);
mtlchk
  • 11
  • 6
  • Have you put "process:" at the place of "//Prompt" ? – g.momo Feb 07 '21 at 01:01
  • It's the fourth line: process: nbPlayers = keyboard.nextInt(); – mtlchk Feb 07 '21 at 01:03
  • Sorry, I meant put it before "do {" so it will become "process: do {" – g.momo Feb 07 '21 at 01:09
  • I would avoid using labels unless you are absolutely required to use them. They lead to the creation of very hard to maintain code. – Hovercraft Full Of Eels Feb 07 '21 at 01:13
  • In fact, well reading your problem, labels are not supposed to be used this way. They are most for terminating a loop. I advice you to use "boolean process = true;" instead, then transform "break process" by "process = false", and insert an "if" before your last "if". Ex: "if(process) { .... if(attempts < 4 && nbPlayers >= 2 && nbPlayers <= 4) {" – g.momo Feb 07 '21 at 01:15
  • Well, that compiles, but it produces the same problem as using continue without a label did. – mtlchk Feb 07 '21 at 01:15
  • What "continue" does ? What did you expect "break" to do ? It will help to fix your algorithm – g.momo Feb 07 '21 at 01:18
  • other advices: (1) `++attempts` before `break process:` should be removed (2) `System.exit(0)` should be removed also because the `else if (attemps = =4)` will do the job – g.momo Feb 07 '21 at 01:25
  • Well, put simply I wanted it not to terminate if someone entered a non-int. – mtlchk Feb 07 '21 at 01:38
  • Okay, have a look on the code Im about to share in a response below, we will discuss on it. – g.momo Feb 07 '21 at 01:40

1 Answers1

1

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.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
g.momo
  • 536
  • 2
  • 7
  • I copied it exactly and commented out my loop. I get this output. I don't understand this behaviour at all :/ --- Enter the number of players for your game. Number must... 2 and 4 inclusively:--- 9 --- Bad Attempt 1. Wrong input type. Please enter an integer between 2 and 4 inclusively.--- Bad Attempt 2. Wrong ... Please enter an integer between 2 and 4 inclusively.--- Bad Attempt 3. Wrong ... Please enter an integer between 2 and 4 inclusively.--- Bad Attempt 4. Wrong ... Please enter an integer between 2 and 4 inclusively.--- You have exhausted.. chances. Program will terminate! – mtlchk Feb 07 '21 at 01:57
  • 1
    It was a copy-paste problem with **Wrong input type** part. I just updated. Tell me more about the values you enter – g.momo Feb 07 '21 at 01:58
  • 1
    Now what do you think ? – g.momo Feb 07 '21 at 02:02
  • Yes, the initializations are the same as yours. This is what happens:--- Enter the number of players for your game. Number must be between 2 and 4 inclusively:--- 9--- Bad Attempt 1. Wrong input type. Please enter an integer between 2 and 4 inclusively.--- 7--- Bad Attempt 2. Wrong input type. Please enter an integer between 2 and 4 inclusively.--- p--- Bad Attempt 3. Wrong ... Please enter an integer between 2 and 4 inclusively.--- Bad Attempt 4. Wrong input type. Please enter an integer between 2 and 4 inclusively.--- You have exhausted all your chances. Program will terminate! – mtlchk Feb 07 '21 at 02:03
  • 1
    And now with the updated version ? – g.momo Feb 07 '21 at 02:05
  • I don't know why it executes the loop without accepting new input from the user once a non-int is entered and the catch block was called. I have been fiddling with this for a long time, and I have had several versions of output that I don't understand. – mtlchk Feb 07 '21 at 02:08
  • 1
    What is the type of keyboard ? Is it Scanner ? If yes, try to .nextLine(), then parseInt it – g.momo Feb 07 '21 at 02:14
  • Scanner keyboard = new Scanner(System.in); I get similar output as before. Once a char is entered it won't pause to accept new input from the user-- Enter the number of players for your game. Number must be between 2 and 4 inclusively:--- 0--- Bad Attempt 1. Invalid number of players.--- p--- Bad Attempt 2. Wrong input type. --- Bad Attempt 3. Wrong input type. --- Bad Attempt 4. Wrong input type. --- You have exhausted all your chances. Program will terminate! – mtlchk Feb 07 '21 at 02:18
  • 1
    Test again now... – g.momo Feb 07 '21 at 02:21
  • Oh, wow, I didn't even know you could do that. It does work, and thanks so much. Can you explain why it won't accept new user input after the catch block is executed? I am stumped. – mtlchk Feb 07 '21 at 02:31
  • 1
    It is a cache problem with some input consoles. In your case, after reading an invalid value (string instead of int) it kept it in its cache, so in following attempts to read your input, it directly considered the cached value. – g.momo Feb 07 '21 at 02:37
  • I was thinking that the error prevented the same method from executing again, but that's just a guess. Also I probably do not understand what try - catch is good for ^^ It's ok. In spite of the downvotes (I lost the upvoting privilege for this) I still believe that we can learn and grow more quickly from asking questions/making errors like this. – mtlchk Feb 07 '21 at 02:42
  • It came back! I am not sure why as my score is unchanged, but let's not complain about gift horses. :) – mtlchk Feb 07 '21 at 02:57