Prime Directive (even or odd numbers): add to ListArray

Hello everyone

I tried to do the extra exercise at the bottom of the Prime Directive: * Build a method that filters an array for odd or even numbers (bonus points if it can do either depending on arguments passed in!).

This is my code:

import java.util.ArrayList;

public class EvenOrOdd {
  boolean add = true;

  public boolean even(int number) {
    if (number % 2 == 0) {
      return true;
     return false;

  public ArrayList<Integer> AddToList(int[] numbers, String task) {

if (task == "even") {
   add = true; }
  else if (task == "oneven") {
     add = false;

    ArrayList<Integer> evenNumbers = new ArrayList<Integer>();
    ArrayList<Integer> oddNumbers = new ArrayList<Integer>();

   for (int everyNumber : numbers) {
if (even(everyNumber) == true && add == true) {
return evenNumbers;
} else if (even(everyNumber) == false && add == false) {
return oddNumbers;
} else {

return oddNumbers;

  public static void main (String[] args) {
  EvenOrOdd test = new EvenOrOdd();
  int[] listOfNumbers = {1, 2, 3, 4, 5};

System.out.println(test.AddToList(listOfNumbers, "even"));


Somehow, my output is always [2]. I suppose there’s something wrong with my ‘for each’ loop, but I can’t seem to figure it out. How do I make sure that every instance is tested and, if necessary, added to the array?

I’m not sure that the return should be in the loop, because that would end the loop and the function right then.
Maybe it should be after the loop?

That helped me figure it out, thanks!