I’m having this interesting ArrayIndexOutofBounds exception.
The code is this:
int m = 0;
while (scan.hasNextInt())
{
intArray[m++] = scan.NextInt();
}
I’m merely scanning a bunch of integers into an array, but I always get an ArrayIndexOutofBounds error. I thought my int m was already initialized to zero?
Thanks
inputString.length()returns the number of characters in the string. it does not necessarily correspond to the number of numbers in your string.You will want to add another condition to yourwhilestatement to ensure thatmdoesn’t get larger thanintArray.length. Also you probably want to step through the code with a debugger to determine exactly when the array runs out of space.Since java arrays are fixed size, if you don’t know what the size of your input is going to be, you should instead use
ArrayList<Integer>to store your input.