Exception on stream java.lang.ArrayIndexOutOfBoundsException: 5

I am a newbie trying to follow the tutorial below

    // Create a method called countEvens
    // Return the number of even ints in the given array. 
    // Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1. 
/*
 * SAMPLE OUTPUT:
 *  
 * 3
 * 0
 * 2
 *  
 */

      

Below is my code

public static void main(String[] args) {

        int a[] = {2, 1, 2, 3, 4};
         countEvens(a); // -> 3
         int b[] = {2, 2, 0};
         countEvens(b); // -> 3
         int c[] = { 1, 3, 5};
         countEvens(c); // -> 0

    }


    public static void countEvens(int[] x){
        int i = 1;
        int count = 0;
        while ( i <= x.length){
            if (x[i] % 2 == 0){
                count ++;
            }
            i ++;
        }
        System.out.println(count);
    }

      

The code can run, but I am getting the following error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at apollo.exercises.ch05_conditionals.Ex5_CountEvens.countEvens(Ex5_CountEvens.java:23)
    at apollo.exercises.ch05_conditionals.Ex5_CountEvens.main(Ex5_CountEvens.java:10)

      

May I know what I am doing wrong here?

+3


source to share


4 answers


In while ( i <= x.length)

you loop until i

it equals the length x

. The last index of an array is always -1 long, so change the less than or equal to ( <=

) value to a value less than ( <

). Also, initialize i

before 0

as Java arrays are zero based.



+1


source


Line

while ( i <= x.length)

      

it should be

while ( i < x.length)

      



If length

of x

is 5, for example, the indices are 0, 1, 2, 3, and 4. The index goes from 0 to one less than the length of the array.

However, the easiest way to do this is to use for each loop, not a while loop:

public static void countEvens(int[] x) {
    int count = 0;
    for (int number : x)
        if (number % 2 == 0)
            count++;
    System.out.println(count);
}

      

+3


source


'i' must go from 0 to length () - 1 , because array indices start at 0, and the index of the last element is length () - 1.

Hence the correct version of your code would be:

public static void countEvens(int[] x){
    int i = 0;
    int count = 0;
    while ( i < x.length){
        if (x[i] % 2 == 0){
            count ++;
        }
        i ++;
    }
    System.out.println(count);
}

      

For your specific purpose, the for loop will be simpler.

for(int i = 0; i< x.length(); i++){
  if(x[i]%2==0){
  count++;
  }
}

      

+2


source


Arrays in Java (and most other languages) are indexed starting at 0. However, the length of an array is the count of the elements in the array. So, for your array []

int a[] = {2,1,2,3,4};

      

The index increases to 4 and the length is 5.

You are getting an out of bounds index error because you are iterating through the array from 1 to 5 due to the <=

(less than) operator , but the array indices are 0 ~ 4. When you are accessing each element of the array, you must use

if (x[i-1] % 2 == 0) //iterates from 0~4 rather than 1~5

      

otherwise you can set the iterator int i = 0;

and use the less operator <

to ensure that the iterator moves 0 ~ 4

0


source







All Articles