The Prime Directive Error - missing return statement

Can someone please explain to me why I’m receiving the above error message? Thanks

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

     } else if (number % i != 0){

       return true;

     }

   }

  }

  public static void main(String[] args) {

    PrimeDirective pd = new PrimeDirective();

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

  }  

}

In Java all paths must eventually lead to a return. In the event that none of your if or else if conditions evaluate to truthy, there is no default return. The compiler only sees that every return is conditional.

I’ve made a few tweeks to both if else statements, and I’m still getting the same error message. Where am I missing a return statement please?

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;

   } else {

     System.out.println("Error");

   }

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

     if (number % i == 0) {

       return false;

     } else if (number % i != 0){

       return true;

     } else {

       return false;

     }

   }

  }

  public static void main(String[] args) {

    PrimeDirective pd = new PrimeDirective();

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

  }  

}

You need a return true outside the loop and outside any if or else-if or else blocks, at the end of the function, so the compiler has something to return (and it that be true, so that if no factors/divisors were found, you have true for being prime).

Some of the other returns in the code are not necessary:
the return in the else-if block and the return in the else block should not be there
because you want the loop to keep going even if the first number it checks for i (which is 2) is not a factor of number.

(By factor or divisor of number, I mean something that divides number evenly into an integer, so the remainder would be 0.)

The compiler is complaining that you don’t have a return that is guaranteed to be reached. All of your return statements are only reached based on a condition evaluating to truthy. Consider the following.

Example:
public class IsMe {
  public boolean isMe(String n) {
    if( n == "Tod") {
      return true;
    } else if (n != "Tod") {
      return false;
    }
  }
  public static void main(String[] args) {
    IsMe iM = new IsMe();
    System.out.println(iM.isMe("Tod"));
  }  
}

Try compiling:

$ javac IsMe.java
IsMe.java:8: error: missing return statement
}
^
1 error
$

Two ways to fix:
Either:

public class IsMe {
  public boolean isMe(String n) {
    if( n == "Tod") {
      return true;
    } else {
    return false; //No condition. If the above return isn't reached, this one will be for sure.
    }
  }
  public static void main(String[] args) {
    IsMe iM = new IsMe();
    System.out.println(iM.isMe("Tod"));
  }  
}

Try compiling & executing:

$ javac IsMe.java
$ java IsMe
true
$

Or using a default return:

public class IsMe {
  public boolean isMe(String n) {
    if( n == "Tod") {
      return true;
    }
    return false; //default return
  }
  public static void main(String[] args) {
    IsMe iM = new IsMe();
    System.out.println(iM.isMe("Larry"));
  }  
}

Try compiling & executing:

$ javac IsMe.java
$ java IsMe
false
$

I need some help with this project as well. I wrote my code like this:
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; number < 2; i++){
  if (number % 1 == 0){
    return false;
  }
}
return true;

}

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(7));
System.out.println(pd.isPrime(28));
System.out.println(pd.isPrime(2));
System.out.println(pd.isPrime(0));

}

}

the problem is when i try to run the code is come out with true true true false.
when 28 should come out to be false.
Did i code something wrong>

Your for-loop and the if-statement in it has the wrong conditions …
You need to check if there’s any integer i below number that id a divisor/factor for number.

Also, every integer is divisible by 1, so number % 1 == 0 will always be true.
Instead this should be number % i == 0 to see if there’s a remainder of 0 when you divide by different values for i
to check whether i is a factor of number.

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

Thanks you! Yea I found my mistake after i posted that lol But thank you for the help!