The Prime Directive

I am struggling with this project as when I put in the code, I get this error:

PrimeDirective.java:19: error: cannot find symbol
    if (number == prime) {
                  ^
  symbol:   variable prime
  location: class PrimeDirective
1 error

This my code by the way:

import java.util.ArrayList;

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;
      }
    }
    if (number == prime) {
      return true;
    } else {
      return false;
    }
  }
  
  
  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(6));
  }  

}

Your error message tells you exactly what the problem is. To the computer prime is just a variable with nothing assigned to it that has also not been declared. You don’t need that if statement at all in your isPrime() function. The code preceding that block already determines whether or not a number is prime. You do have a problem in your for loop though that will prevent your function from working properly. If you want the for loop to iterate over all intended values you can’t have an if ... else statement inside the for loop that has a return statement for both conditions. Only the first value will be checked, and then return will exit the loop.

2 Likes

So what should my code for IsPrime() be then.

@midlindner has already given you a point in the right direction. You’re not learning if we give you the code outright, now. :slight_smile:

2 Likes

Here’s a hint if you’re still stuck, but as @thepitycoder pointed out,

See what you can do, and post again if you need more help.

2 Likes

So get rid of the return true statement after the else in the second if statement.

You still need to return true, but not until after you have iterated through all possible values of i, so the return true should come after the for loop has completely finished.

1 Like

My current error is that prime variable does not exist in the current context.

Yes. You need to delete that entire if block:

    if (number == prime) { //this whole block needs to be removed from here ...
      return true; // ...
    } else { // ...
      return false; // ...
    } // ... to here

You function will determine whether a number is prime without this. prime is an undefined variable in this context, and it isn’t necessary. If there is some instruction in the project that asks you to use prime, post a link to the project.

2 Likes

Okay I got rid of that if block but now the program does not print anything.

Could you post all of your code, so we can see what you’ve got now?

Here’s my current code:

import java.util.ArrayList;

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;
      }
    }
    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(6));
  }  

}

I just ran your code, and got false printed as output which is correct for the number 6. The code wouldn’t run in my environment until I added public in front of class PrimeDirective {.
You might give that a try.

3 Likes

Post a link to the project, please.

https://www.codecademy.com/courses/learn-java/projects/java-prime-directive

I used the other code runner and it actually worked. Thanks a mill, mate.

1 Like

Cool. I just copied your code into the project, and ran it as expected:

3 Likes

does anyone have the solution for only primes?

Hello, @shenon_1987. Welcome to the forum. Many learners have passed the exercise, and ‘have’ the solution so to speak. We do not simply post solutions to the forum. That sort of defeats the purpose of teaching. If you have a specific question, or would like help with your code, please ask, or post your code, and we’ll assist you in discovering the solution for yourself.

1 Like