Where is my missing return statement?

Having some difficulties figuring out where my missing return statement is in The Prime Directive exercise. This is preventing me from moving forward. Returns are something that my brain is still struggling to wrap my head around. Any advice on how to fix my mistake would be greatly appreciated.

Here is the error code the compiler is giving me:

PrimeDirective.java:31: error: missing return statement
}
^
1 error

// Import statement:

import java.util.ArrayList;

class PrimeDirective {

  

  // Add your methods here:

  public boolean isPrime(int number) {

    if (number == 2) {

       return true;

    } else if (number < 2) {

      return false;

    }

for (int i = 2; i < number; i++) {

if (number % i == 0) {

return false;

  }

  }

  }

    

  public static void main(String[] args) {

    PrimeDirective pd = new PrimeDirective();

    int[] numbers = {6, 29, 28, 33, 11, 100, 101, 43, 89};

System.out.println(pd.isPrime(6));

  }  

  }
1 Like

I’ve reformatted your brackets a little to clarify intention. The real question is, does the compiler know your method is guaranteed to return a value? If it doesn’t, you need to make it explicitly return a value (in this case, boolean) as a default case at the end (in case the control-flow falls through).

public boolean isPrime(int number) {

    if (number == 2) {
       return true;
    } else if (number < 2) {
      return false;
    }

    for (int i = 2; i < number; i++) {
        if (number % i == 0) {
            return false;
        }
    }
}

2 Likes

Thanks for the reply and organizing my brackets more cleanly!

I’m not sure which statement I need to complete with a return value that hasn’t been completed already?

the 1st if statement is completed with a return true and return false, should I complete the 2nd one with an else statement and return true;?

    for (int i = 2; i < number; i++) {
        if (number % i == 0) {
            return false;
        } else {
return true;
}

I’m not sure what you mean by default or how to output the default case at the end. Do I need to format it like a switch statement?

Maybe I need to go through the if else tutorial again to solidify the concept.

Or can I use a break statement to tell the program to exit the for each loop?

public static boolean isPrime(int number) {

    if (number == 2) {

       return true;

        } else if (number < 2) {

         return false;

    }

    for (int i = 2; i < number; i++) {

        if (number % i == 0) {

            return false;

            break;

            

              }  

          }

    }

or do I exit the statement by saying return true;?

public static boolean isPrime(int number) {

    if (number == 2) {

       return true;

        } else if (number < 2) {

         return false;

    }

    for (int i = 2; i < number; i++) {

        if (number % i == 0) {

            return false;

              }  else {

                return true;

              }

          }

    }

Hi,

Sorry if the terminology was confusing, I didn’t mean to use a switch statement – but I did mean very much like a switch statement to provide the method with something to do in case all the alternatives are false.

How you pull it off is up to you. You don’t need a break after return (as return already breaks the loop and method).

else {
  return true;
}

Could be a possible way for it to not throw an error. Although you would need to consider the logic behind using it and its placement. I would consider erring on the side of return false at the end of the method and then refactor it if its not necessary that deep in.

Okay, so I corrected the if-else statement to return true in the right spot; but then I was not printing anything until I added another return false statement like you suggested.

public boolean isPrime(int number) {

    if (number == 2) {

       return true;

    } else if (number < 2) {

      return false;

    }

for (int i = 2; i < number; i++) {

if (number % i == 0) {

return false;

  } else {

    

    return true;

      }

    }

  return false;

  }

So this return false statement in the brackets of the isPrime method effectively ends the boolean method?

I’m confused why my program would need this return false statement if the .isPrime() method is already using the first if (number % i == 0) to execute the return false code on the number 6. Is it just a fail safe that the compiler requires so it doesn’t go into an infinite loop?

Hm, we can work backwards with assumptions.
Each method must return what its definition promises. This is a high-priority check to compile.

I’m guessing anything that throws ambiguity as to whether the promised return is existent throws an error. So unless there’s a control flow with a guaranteed else statement that returns a value, the compiler would have to either unit test or break its own rule about returns to validate the method.

That’s what I think is happening. It’s been a while since I touched java seriously.