If i am looking for a certain value (needle) in an arrary and the value doens't exit in this array I am getting an java.lang.ArrayIndexOutOfBoundsException. (see Code below) If the value exists in the array it works just fine.
It seems I am sitting already to long in front of my computer and i am already to blind to see the mistake, any help would be really appreciated. Thank you!
public static void main(String[] args) {
int i = 0;
int[] array;
array = new int[5];
int needle = 20;
boolean inarray;
array[0] = 4;
array[1] = 7;
array[2] = 13;
array[3] = 29;
array[4] = 5;
while (i <= array.length && needle != array[i]){
i++;
}
inarray = !(i > array.length);
System.out.println("value in array: " + inarray);
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at test.Test.main(Test.java:33) C:\Users\admin\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)