Even/odd values in an array

Viewed 1179

I am having trouble with a boolean method, I want to check of the array is all even, odd, or neither. I input the array size and array values, however the "isArrayEven" method keeps outputting "all numbers in array is even", even if my array were to be 1,2,3 and isArrayEven is supposed to be false.

import java.util.Scanner;

public class OddOrEven {
    
    public static boolean isArrayEven(int[] arrayValues, int arraySize){

        for(int i = 0; i <= arraySize -1; i++)
        {
            if(arrayValues[i] % 2 == 0)
            {
                return true;
            }
        }
        return false;
    }

    public static boolean isArrayOdd(int[] arrayValues, int arraySize){

        for(int i = 0 ; i < arraySize ; i++)
        {
            if(arrayValues[i] % 2 == 1)
            {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        
        Scanner scan = new Scanner(System.in);

        int arraySize = scan.nextInt();             
        int[] arrayValues = new int[arraySize];     

        for(int i = 0; i <= arraySize-1; i++)
        {
            arrayValues[i] = scan.nextInt();
        }
        
        if(isArrayEven(arrayValues,arraySize) == true)
        {
            System.out.println("all numbers in array is even");
        }else if(isArrayOdd(arrayValues,arraySize) == true)
        {
            System.out.println("all numbers in arrat is odd");
        }else if(isArrayEven(arrayValues,arraySize) == false && isArrayOdd(arrayValues,arraySize) == false)
        {
            System.out.println("both have even and odd");
        }
    }

}

I

5 Answers

You are returning too early. You need to flip and modify your if-statements a bit. Have a look at this:

public static boolean isArrayEven(int[] arrayValues, int arraySize){

    for(int i = 0; i <= arraySize -1; i++)
    {
        if(arrayValues[i] % 2 != 0)
        {
            return false;
        }
    }
    return true;
}

public static boolean isArrayOdd(int[] arrayValues, int arraySize){

    for(int i = 0 ; i < arraySize ; i++)
    {
        if(arrayValues[i] % 2 == 0)
        {
            return false;
        }
    }
    return true;
}

Here I have modified the if-statements a bit.

In the isArrayEven method, I return false on the first occurrence of an odd number. If no odd number occurs, I return true.

In the isArrayOdd method, I return false on the first occurrence of an even number. If no even number occurs, I return true.

It seems to me your tests and return values should be inverted. You will now return "the entire array is even"=true when you find just one even number. You have to check all values, or return false when you find any value that is not even in the "is even" function.

In general I would suggest it would be better to iterate your array once and initialize two bools before iterating; eg. "all_are_odd" and "all_are_even" and initialize these to true;

Then just iterate the full array once and only set all_are_x to false if you come across a value that does not meet all_are_x.

You could optimize by breaking out of the loop once both all_are_odd / all_are_even are false.

After this iteration you can now output whether all are even, all are odd, or something in between.

Edge case is of course the empty set.

your wrong is you are returning true or false whenever you find even number or odd number. That means lets say you have an array of integer numbers [2, 1, 3, 5]. When your isEven method ran for this array it will return true, but the 1st, 2nd and 3rd values of the array is odd. What you need to do is the opposite of you are doing right now.

public boolean isEven(int arr[]){
    for(int i=0; i<arr.length; ++i)
       if(arr[i]%2 !=0) return false;
    return true;
}

You should send false if you find odd number in array and return true if all element in array is even.

public static boolean isArrayEven(int[] arrayValues, int arraySize){
    for(int i = 0; i <= arraySize -1; i++) {
        if(arrayValues[i] % 2 != 0) { // if found an odd number
            return false;
        }
    }
    return true; // if not found any odd number means all number is even
}

I sort of agree with Richard in that you need some boolean variables, but I would take a different approach:

boolean containsEven = false; boolean containsOdd = false;

Cycle through all the elements in the array, and as soon as you find an even number change the first boolean to true. Likewise, change the second boolean to true once an odd number has been found. You might even consider breaking the cycle early if both booleans become true, as there would be nothing further to learn from inspecting the rest of the array.

Then, use these booleans in your if/else block.

Related