Prime directive

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

class PrimeDirective {
  
  // Add your methods here:
  public boolean isPrime(int x){
    if(x == 2) return true;
    if(x < 2) return false; 

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


  public ArrayList<Integer> onlyPrime(int[] numbers){
    ArrayList<Integer> PrimeNumbers = new ArrayList<Integer>();
    for(int i:numbers){
      this.isPrime(i);
      if(isPrime(i)){
      PrimeNumbers.add(i);
      
      }
  }
      return PrimeNumbers;
    }
  

  public static void main(String[] args) {

    PrimeDirective pd = new PrimeDirective();
    System.out.println(pd.onlyPrime(numbers));
    int[] numbers = {6, 29, 28, 33, 11, 100, 101, 43, 89};
  

  }
  }

I am getiing error cannot find symbol
System.out.println(pd.onlyPrime(numbers));
^

Blockquote

1 Like

Thanks for sharing!

You’re defining numbers after your println. If you define it before (switch the lines) the code will run smoothly.

1 Like

My print looks like that:

 javac PrimeDirective.java
$ java PrimeDirective

false
false
true
true
[29, 33, 11, 101, 43, 89]
But int the hint is pointed another case: // should print [29, 11, 101, 43, 89]
Probably my code is incomplete. There is something missing in it. Would you guess what is this?

My code link is here : PrimeDirective - Pastebin.com