# Prime Directive

Continuing the discussion from Help with PrimeDirective.java:

Hi!

I did the obligatory part of PrimeDirective and the code ran just fine. I also built a method to filter the numbers array for odd numbers. That worked, too.

When, however, I tried to create two separate ArrayLists for odd and even numbers and return them both from the same method (oddOrEven), the programme disregarded the ‘return oddNumbers’ command and kept returning the even numbers only. It did so even after I deleted the ‘return evenNumbers’ command. Somehow, it wouldn’t print the ArrayList containing the odd numbers!

Where could I have gone wrong? Is it not possible to return 2 things from the same piece of code? Thank you for your help!

Here’s my code:

import java.util.ArrayList;

class PrimeDirective {

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 onlyPrimes(int numbers){

ArrayList primes = new ArrayList();

for (int number : numbers){

if (isPrime(number)){

}

}

return primes;

}

public boolean isOdd (int number){

if (number % 2 != 0) {

return true;

}

else {

return false;

}

}

public ArrayList oddNumbers (int numbers){

ArrayList onlyOdds = new ArrayList();

for (int number : numbers){

if (isOdd(number)){

}

}

return onlyOdds;

}

public ArrayList oddOrEven (int numbers){

ArrayList oddNumbers = new ArrayList();

ArrayList evenNumbers = new ArrayList();

for (int number : numbers){

if (isOdd(number)){

else {

}

}

return oddNumbers;

return evenNumbers;

}

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.oddOrEven(numbers));

}

}

import java.util.ArrayList;

class PrimeDirective {

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 onlyPrimes(int numbers){

ArrayList primes = new ArrayList();

for (int number : numbers){

if (isPrime(number)){

}

}

return primes;

}

public boolean isOdd (int number){

if (number % 2 != 0) {

return true;

}

else {

return false;

}

}

public ArrayList oddNumbers (int numbers){

ArrayList onlyOdds = new ArrayList();

for (int number : numbers){

if (isOdd(number)){

}

}

return onlyOdds;

}

public ArrayList oddOrEven (int numbers){

ArrayList oddNumbers = new ArrayList();

ArrayList evenNumbers = new ArrayList();

for (int number : numbers){

if (isOdd(number)){

else {

}

}

return oddNumbers;

return evenNumbers;

}

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.oddOrEven(numbers));

}

}

I see a problem in the `oddOrEven` function.

This function has two returns,
but a function can only return one thing,
and functions stop when they reach a return,
so the second return would never be executed;

I suggested having separate functions, one for getting the odd numbers, and one for the getting the even numbers.
Or, you could have a boolean parameter that you’d use to decide whether to return `oddNumbers` or `evenNumbers`.

Another alternative is to have the function return an array of ArrayLists:

``````public ArrayList[] oddOrEven (int numbers){
``````

and at the end of the function, you would do:

``````return new ArrayList[]{ oddNumbers, evenNumbers};
``````
1 Like

As mentioned by @blackace4657354007, the parameter is an array, not a single integer; so there should be a `[]`

``````public ArrayList[] oddOrEven (int[] numbers){
``````