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

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