0

I'm having an assignment where I need to count the prime numbers in an array list. There are several solutions to this here on Stackoverflow but I need to do it in another way. I have the initializing of the array list int the method head itself, not the body. I believe there is a difference but I might be wrong.

I need to use this method:

public static int countPrimeNumbers(ArrayList<Integer> list) {

}

So far I'm coming up with this code:

public static int countPrimeNumbers(ArrayList<Integer> list) {
    list = new ArrayList<Integer>();

    int n = 0;
    int count = 0;
    System.out.println(list);

    for(int i=2; i<list.size(); i++) {
        if(n%i==0)
            count++;
    }
    return count;

}

But I'm not sure if it's somwehere near completion since I don't know how to call it from Main. Regards

Thomas Bengtsson
  • 399
  • 2
  • 10
  • 22
  • In the first line of code you're discarding the input! Shouldn't you be looking at what integers are passed to the method? I would suggest you implement a `static boolean isPrime(int i) { ... }` and then do something like `return (int) list.stream().filter(YourClass::isPrime).count();`. – aioobe Feb 17 '18 at 14:09
  • You do need to just call `printcountnum` method. Thats it – atiqkhaled Feb 17 '18 at 14:09
  • `n` is always zero here – OneCricketeer Feb 17 '18 at 14:16
  • @aioobe Could you please elaborate? I'm afraid I'm not sure what you mean. – Thomas Bengtsson Feb 17 '18 at 14:16
  • "You're discarding the input"... You have a list as a parameter, then you immediately reassign the value to a completely different, empty list. Your for loop will loop over nothing, and count nothing – OneCricketeer Feb 17 '18 at 14:18
  • "I don't know how to call it from Main" => [Initialization of an ArrayList in one line](https://stackoverflow.com/q/1005073). The best way forward is probably to [debug](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) it - us explaining what's wrong with it won't really help you with the next issue you'll have. – Bernhard Barker Feb 17 '18 at 14:20

1 Answers1

2

When you write this list = new ArrayList<Integer>(); you are discarding the list passed in to the method as parameter. This is how you can call it from main method.

private static int countPrimeNumbers(List<Integer> list) {
    int count = 0;
    for (int i : list) {
        if (isPrime(i)) {
            count += 1;
        }
    }
    return count;
}

// FYI... logic to find prime number can further be optimised, i will leave it upto you
private static boolean isPrime(int number) {
    for(int i = 2; i < number; i++) {
        if(number % i == 0) {
            return false;
        }
    }
    return true;
}

public static void main(String args[]) {
    Integer[] numbers = {2, 3, 5, 6, 10};
    List<Integer> list = Arrays.asList(numbers);

    int result = countPrimeNumbers(list);

    System.out.println("Number of prime numbers = " + result);
}
Rishikesh Dhokare
  • 3,559
  • 23
  • 34