1

I'm trying to use a method to display an error message if the user enters a number other than 1 - 4, but I'm getting a missing return statement error.

public int CheckAnswers () {
    boolean incorrectAnswer = true;

    do {
        playerAnswer = CheckAnswers();
         if (playerAnswer < 1 || playerAnswer > 4) {
            System.out.println("You have entered an incorrect number.");
            System.out.println("Please enter a  number between 1 and 4");
         } else {
               return (playerAnswer); }

        } while (incorrectAnswer);

   }

The error points to the last bracket. I've done some looking around online and I think the problem is that I don't have a return statement in both parts of the if-else statement. But if they have entered an incorrect number I don't want to return anything. I tried using the below code unsuccessfully.

public int CheckAnswers () {
    boolean incorrectAnswer = true;

    do {
        playerAnswer = CheckAnswers();
         if (playerAnswer < 1 || playerAnswer > 4) {
            System.out.println("You have entered an incorrect number.");
            System.out.println("Please enter a  number between 1 and 4");
              return (null);
         } else {
               return (playerAnswer); }

        } while (incorrectAnswer);

   }
0

4 Answers 4

3

The compiler's analysis does not determine that

do {
    playerAnswer = CheckAnswers();
     if (playerAnswer < 1 || playerAnswer > 4) {
        System.out.println("You have entered an incorrect number.");
        System.out.println("Please enter a  number between 1 and 4");
     } else {
           return (playerAnswer); }

    } while (incorrectAnswer);

is indeed an infinite loop, and the only way to get out of the loop is the return in the else branch. (Since incorrectAnswer cannot be changed except by cosmic rays flipping bits, it is, but the compiler isn't convinced.)

Thus it wants a return in case the loop is left in a different way.

If you make the loop condition a literal true,

do {
    // code
} while(true);

the compiler will know that the loop can only be left via the return in the else branch (you can and should eliminate the boolean incorrectAnswer; then).

As yannis hristofakis observed, calling playerAnswer = CheckAnswers(); immediately in the loop causes an infinite recursion, and that will lead to a stack overflow. You need to call a method that obtains some input from the user instead there.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm amazed that 3 answers were posted and none of them mentioned that

  1. It's an eternal procedure since you call recursevly the CheckAnswers method.

  2. You can't return null for primitive return type methods.

I think you should skip the parenthesis.

return playerAnswer;

1 Comment

Point 1 (good observation, by the way) has nothing to do with the compilation error. It will lead to a stack overflow when the thing compiles and is run, though.
0

Just do a return statement of return -1 right after the do while. You will not ever reach that code, but it will make the compiler happy who is not able to understand the logic of your code and just sees that there are possible execution paths that don't return anything.

public int CheckAnswers () {
    boolean incorrectAnswer = true;

    do {
        playerAnswer = CheckAnswers();
        if (playerAnswer < 1 || playerAnswer > 4) {
          System.out.println("You have entered an incorrect number.");
          System.out.println("Please enter a  number between 1 and 4");
        } 
        else {
           return (playerAnswer); 
        }
    } while (incorrectAnswer);
    return -1; //unreachable statement
}

Comments

0

Try this:

public int CheckAnswers () {
    boolean incorrectAnswer = true;

    do {
       playerAnswer = CheckAnswers();
       if (playerAnswer < 1 || playerAnswer > 4) {
            System.out.println("You have entered an incorrect number.");
            System.out.println("Please enter a  number between 1 and 4");
       } else {
            incorrectAnswer = false;
       }
    } while (incorrectAnswer);

    return playerAnswer;
}

1 Comment

Thank you to everyone who answered! The problem is solved! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.