The Prime Directive

My Prime Directive always returns “true” which makes sense since the “return true;” line is written after the loop. But that’s exactly what the instruction tells me to do, otherwise, I get an error for not returning anything. Can someone please explain to me why this doesn’t work?

// 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 (i % number == 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(28));

}

}

Hi - welcome to the forums!

Does that look right to you? :slight_smile:

Thank you hahaha, it was backwards… I feel dumb now.

1 Like

Don’t beat yourself up too bad. Little mistakes like that are often some of the hardest to spot after you’ve been staring at the same program for ages.

You’re not the first to do something like that, and you sure won’t be the last. :+1:t2:

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.