Prime number checker

From Exercise:
https://www.codecademy.com/paths/introduction-to-android-with-java/tracks/java-arrays-and-loops/modules/learn-java-loops/projects/java-prime-directive

Something’s off with my prime number checking method:
33 returns true (the method finds that it is a prime number)! But the output is correct for all other numbers I’ve tried.
What am I not seeing?

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

Try running a trace. Which of your conditionals is 33 triggering? That’s the clue.

hint: there’s one return that shouldn’t be there

I have error: “missing return statement” in my code then I test your code, I found that my missing return is the last return you wrote. you code is running without i++, that is why all the number equals 3times, 5times, 7times won’t be detected.

To solve this, we could not judge true for odd number, for odd number, we should use continue to keep loop with i++, once all the integer < number being tested, return true after that.

Thankyou for give me idea!
Happy coding~

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

dear Sir, I find a kinda intimidating amount of info about running a trace in Java. Would you have instructions for me how to do that? Did you mean something like this? Runtime.traceMethodCalls()
Can’t remember which lesson this was covered in.

Hello @ byte4021799113, It’s great you’re here!
Fun fact, That same error message “missing return statement” is the only reason why I put it there - and now, when I comment it out, I get the same output as with it?

[quote=“byte4021799113, post:4, topic:627582”]
you code is running without i++, that is why all the number equals 3times, 5times, 7times won’t be detected.
[/quote] What do you mean without i++? Where would you put that?

I can’t follow. Could you indicate line numbers?
Here’s the whole thing from me:

import java.util.ArrayList;
class PrimeDirective {
  public boolean isPrime (int number) {
    if (number == 2) {
      return true;
    } else if (number < 2) {
      return false;
    } else {
      for (int i = 2; i < number; i++) {
      if (number % i == 0) {
        return false;
      }
        return true;
      }
    }
    //return true;
  }
  public ArrayList<Integer> onlyPrimes (int[] numbers) {
     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.onlyPrimes(numbers));
  }
}

Good Morning, turn on computer to help you :grinning:(the line number is based on my code, I can’t see your code line number)

Actually our code both have same problem. missing return statement in for loop(ln No.13). when we add return(ln No.27) the error notice disappear. but the output has problem in detecting Number 33.

I check my code by running step by step:
when number =33, in" for loop" (ln 13), i=2, in the following “if condition”(ln 15-23)(33%2!=0) which will return true, see the result shows prime for number 33. it skip the “for loop” (i increment ln.13), as we design, the code should keep looping judge if (33%3==0), then it will detect 33%3=0 and return false.

This was caused by return statement in if-else (ln.21), this “return” will return “true” and skip the rest of code.
so I use “continue” here, then let the for loop finish it works, then return “true”, after all i (from 2 to number-1) being judged.

Oh no I meant like your own trace (either on paper or through print statement).

The point is to isolate the behavior and narrow down the area with the issue to give you a better idea of what’s wrong.

@bennypr0fane Were you able to solve the issue? I just got done with this part and the logic around prime numbers always messes me up (and I’ve been programming for 10 years ha).

I think you may not need that last “else” check. But, I think you may want to remove the duplicate return:

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

Does that help?