# PrimeDirective .isPrime() method is returning 33 as true

Sorry for all the loops questions today!

I’m using http://www.pythontutor.com/visualize.html#mode=display to check all the steps my code runs through.

It’s telling me on step 52 that pd.onlyPrimes(); runs the second integer in the ArrayList 29 and returns the value “true” to the isPrime method, next it returns false for 28.

Then it goes on to run 33 through the method` if (number % i == 0) { return false; }` , then moves to the else statement `else { return true; }`

Obviously I know that this isn’t true, but how do I change it so it doesn’t affect any of the other integers in the ArrayList?

In the end the terminal doesn’t have any errors, but it is printing out 33 as a prime number when it’s not.

Here is the full code:

``````import java.util.ArrayList;

public class PrimeDirective {

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;

}

public ArrayList<Integer> onlyPrimes(int[] numbers) {

ArrayList<Integer> primes = new ArrayList<Integer>();

for (int number : numbers) {

if (isPrime(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.onlyPrimes(numbers));

}
}
``````

Do I need to add another if statement that checks `if (number % 1 == 0 || number % 11 == 0) { return false;)`

I’m not sure how I exclude 33 from the list here.

Any help is appreciated. Loops are destroying me.

**Update: I fixed it thanks to this post Prime Directive - not returning prime number and removed the return false. So it looks like this now:

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

if (number % i == 0) {

return false;

}

}

return true;

}
``````

**

1 Like