prime directive link
Hey everyone, I keep getting a missing return statement } error on line 21. I’m stumped.
Ive checked all my curly brackets. there’s something I’m missing 
Any advice at all would be great 
PrimeDirective.java:21: error: missing return statement
} //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 false;
} else if (number < 2) {
return true;
}
for (int i = 2;i < number; i++){
if (number % i == 0){
return false;
} else {
return true;
}
}
} //missing return statement!!!
public ArrayList<Integer> onlyPrimes(int[] numbers) {
// method body goes here
ArrayList<Integer> primes = new ArrayList<Integer>();
for (int number : numbers){
if (isPrime(number)){
primes.add(number);
}
}
return primes;
}
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));
System.out.println(pd.onlyPrimes(numbers));
}
}
What you’re missing is a quirk of the Java compiler.
If we look at your code, your isPrime
function contains several return
statements - so they’re clearly not missing, right? However, every single return
is nested within a conditional branching structure.
As all your returns are within an if..else-if..else
structure, the Java compiler considers these returns as potentially unreachable. (Whether they’re actually unreachable is irrelevant.) Your function must return a value, but with none of the return statements guaranteed to run the compiler can’t guarantee that your function will provide any output.
To fix it, you need to include a return statement in your function that will absolutely always run regardless of what happens. The easiest way to do this would be to simply add return false;
at the end of the function declaration.
If you’ve coded the function correctly, it’s this return at the end which will be the one that is technically unreachable - and thus never executed - but it will satisfy the compiler that your function will return the boolean it’s promising to, and your error will go away. 
4 Likes
In addition to @thepitycoder, the for-loop needs to be nested inside of the else if. That way if the number is larger than 2, the for-loop runs. This can avoid possible double return statements. Also, change the
} else if (number < 2) {
return true;
}
to
} else (number > 2) {
return true;
}
then add a return false
at the end, if the number is less than 2.
Remember, if you have an if
, then you need an else
or none. There should not be an else if
unless there are multiple conditions.
A simple way of explaining this is if there are two results from an if
. Then when it is one condition it runs within the if
loop, else it runs in the else
loop. If there are three, then it still runs in the if
, but next it goes to the else if
, then the else
. If there are three or more then it will require two less than the number of possible answers. Or use a switch statement.
In this case, there are only two possibilities, it is the same or bigger. (you can make it have three conditions if you want to, but it’s unnecessary) So an if-else
would be the best choice. 


Ive been missing a while due to a move. Thank you for your help. The code ran perfect :grinning:
1 Like
No problem.
The Java docs used to include something about the compiler not liking functions where there wasn’t a definitive “this will run, no matter what” return statement… but for the life of me, I can’t find it now!
Have your codes run correctly for the step 14
( Step 14. Time to put it all together! In main()
, test out pd.onlyPrimes()
on the numbers
array.
Don’t forget to run your code with …)? My codes are the same as yours and mine just don’t work.
Could you post the code? It is very common that someone misses a semicolon or something. Also, post the error so we have an idea on what’s wrong.