Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
i have an array of boolean type
which is
static boolean[][] a = new boolean[50][50];
everytime gets input,
it marks the specified array as true
that is,
for(int i=0; i<k; i++){
int x=sc.nextInt();
int y=sc.nextInt();
a[x][y] = true;
}
but when the number of input, depending on k, gets large,
the following error comes out
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
what is wrong with this
The
Exceptionyou see is because you’re trying to access an element of the array that doesn’t exist (Lies Outside the Bounds).The array you initialize has elements 0…49 in both dimensions. So you can insert values into any position that lies within a[0-49][0-49]
When you do:
There’s the possibliity that you access a value beyond those locations. Such as a negative value or a too high one (In this case you’re accessing -1).
Your issue stems from the fact that
sc.nextInt()is failing to produce a usable integer from your input. How are you initializingsc?