arrays – java.lang.ArrayIndexOutOfBoundsException: 4 Error

arrays – java.lang.ArrayIndexOutOfBoundsException: 4 Error

All of your calls to arrayCharToInt[x+1] are going to go out of bounds on the last iteration of the loop theyre in (for example, if arrayCharToInt.length equals 5, the highest that x is going to go is 4. But then x+1 equals 5, which is out of bounds for an array with five cells). Youll need to put in some sort of if( x == arrayCharToInt.length - 1) check.

in the method isConsecutive inside the for loop: for (int x=0;x<arrayCharToInt.length ;x++ ) , you have used arrayCharToInt[x+1]

if the arrayCharToInt lenth is 4 , then you have arrayCharToInt [0] to arrayCharToInt [3].

now consider this statement:arrayCharToInt[x+1]
when x is 3 this statement will evalueate to arrayCharToInt[4] resulting in array index out of bounds exception

arrays – java.lang.ArrayIndexOutOfBoundsException: 4 Error

This error throw when something went wrong in the Array calling function.
You got the length and make it print.
for eg:

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

Length of this array is,

int length = a.length

So length = 4 but highest index is 3, not 4. That means index of the array started with 0. So you have to print:

arr[length-1];

In your program,

x == arrayCharToInt.length - 1

Leave a Reply

Your email address will not be published.