Help with PrimeDirective.java

Hi, I’m in the middle of doing the Prime Directive project on Learn Java. No matter how many times I check my code for typos or errors it won’t run on java PrimeDirective on the terminal. When I use javac PrimeDirective.java it says I have 13 errors!
Here is my code below:

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 (number % i == 0) {
      return false;
    }
    }
  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.isPrime(6));
    System.out.println(pd.onlyPrimes(numbers));
  }  

}

Any Help or suggestions would be great. Thanks in advance.

Hello, @system4677739580.

Welcome to the forum.

It will be much, much easier to help you if you re-post your code following the guidelines below.

THANK YOU @patrickd314 for this!!

Code that is posted without the indentations, whitespace and special characters preserved, is very difficult to read, and cannot easily be tested by those trying to help you.

The Codecademy forum provides a quick and easy way to post properly-formatted code. All you need to do is look for the </> icon in the menu bar which appears at the top of the text box while you type.

capture

Press Enter to go to a blank line, click that icon, and you will see this:

capture_r

Just copy and paste your code directly from the editor into that highlighted portion, and all of the nicely-readable formatting will be preserved! Moreover, it will be in a format that can be directly copied and pasted into an IDE for testing.

The same icon can be used for highlighting small code snippets in-line for clarity.

Have a look at each closing } and check whether they are all in the right place. If you click on each } it will show which opening { it is connected to. It says you have 13 errors but in reality you only have one.

2 Likes
// Import statement:
import java.util.ArrayList;

class PrimeDirective {
  
  // Add your methods here:

  //isPrime method
  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;
  }
  //onlyPrimes method
  public int onlyPrimes(int numbers) {
    //Declared a new ArrayList called primes
    ArrayList<Integer> primes = new ArrayList<Integer>();
    //for-each loop to find the prime numbers
    for(int number : numbers) {
      //uses isPrime method to check the number in numbers
      if(isPrime(number)) {
        //add the prime number to primes ArrayList
        primes.add(number);
      }
    }
    //return the list of primes
    return primes;
  }

  public int oddAndEvenPrimes(int numbers) {

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

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

    for(number = 0; number < numbers.size(); number++) {
      if(number % 2 != 0) {
        oddPrimes.add(number);
      } else {
        evenPrimes.add(number);
      }
    }
    System.out.println(evenPrimes);
    System.out.println(oddPrimes);
  }
  
  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.oddAndEvenPrimes(numbers));

  }  

}

The terminal is glitchy I think because the code doesn’t compile properly for me. I still get the same response from a old compiled code. False, True, False, True. Even though I removed that print statement.

You have to remember to click Save after making any changes to your code prior to compiling. Otherwise, you just re-compile the same code. (Whatever was saved last.)

I did save the code before the compile.
Would I be using the wrong type in my method declaration. I am using int but should it be boolean or Integer instead perhaps?

Not sure what your issue(s) are. The code you posted won’t compile for me because of a few errors.

Which method are you referring to? In your oddAndEvenPrimes method you specify an int will be returned, but the method has no return statement at all. The parameter for the method is also an int, but it appears you will be providing an array of integers as the argument when you call the method.

Yeah I think I am making a mistake because I am not quite understanding the instructions in the project sometimes.
I believe I was not sure if I was allowed to use a print statement inside a return statement. From what I understand I can only use one or another. But maybe I have that confused with something else.
What errors do you get?
I only got a failed to compile error but it was nothing to do with the code a is.otjerwise when I ran my code I either got nothing or the previous code that was compiled.

10 total errors. Best to start from the beginning.

PrimeDirective.java:27: error: for-each not applicable to expression type
for(int number : numbers) {
^
required: array or java.lang.Iterable
found: int

Look at the method declaration:

What type is numbers? The error message says it’s an int when it needs to be an Iterable like an array.

Yes, I thought that int numbers was just a placeholder for a array of integers. But maybe I am missing the square brackets like when I declare the main method. So maybe
Public integer numbers(Integer numbers) {//code block}

Yes. The parameter should be an array of integers.

public int onlyPrimes(int[] numbers) {

Edit: Adding link to the project for reference: https://www.codecademy.com/courses/learn-java/projects/java-prime-directive