Help with PrimeDetective.java

Hello World…
I’m new in java and i’m totally stuck with my code. I have thinked and watched and my brains just buzzing. Could you please help me? I cant get my if else conditions ( or actually nothing ) working.

-jyri-

// Import statement:
import java.util.ArrayList;
public 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;
      }
    }
  }
  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));

  }  

}
      if (number < 2) {
        return false;
      } else if (number < 2) {
        return true;
      }

Both if and else if share the same condition.

Also return is used to exit from a method, with or without a value. In the current situation the for loop will never be reached.

Thank you very much janneslohmeijer. My brain was exhausted. I didnt see nothing! =) Lets try agein.

1 Like