0

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

4
  • Did you intialized intArray ? Commented Jan 30, 2013 at 4:01
  • Yes, I did initialize my array like this: int[] intArray = new int[inputString.length()]; where inputString is a string i'm scanning for ints. Commented Jan 30, 2013 at 4:03
  • What do you get when you System.out.println(inputString.length()); just before you define the array? Commented Jan 30, 2013 at 4:14
  • p.s. I assume you mean nextInt since NextInt won't compile. Commented Jan 30, 2013 at 4:16

3 Answers 3

4

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 your while statement to ensure that m doesn't get larger than intArray.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.

Sign up to request clarification or add additional context in comments.

2 Comments

Great answer! I never thought about using an ArrayList; but that seems to work a lot better (and a little easier) than just an array!
Good points for sure, but I guarantee the number of numbers is less than the number of characters in the string ;) Think about it. (It would still be nice to know why the array failed, no one got to the bottom of it).
0
  1. Does the array have any elements in it to start off with?

  2. On future iterations of the loop, m is not zero because you increment it inside the loop.

2 Comments

No, my array is initially empty. And yes, I'm aware that m isn't zero anymore after the first iteration.
If your array is initially empty, then array[0] is out of bounds. array[0] is the first element of a zero element array is out of bounds.
0

ArrayIndexOutofBounds means your array isn't big enough to hold the number of values you are putting in it. Where are you initializing the array intArray?

If you don't know how many values the scanner has upfront, which I would assume to be the case, you might want to use an ArrayList instead.

Something like this should work..

List<Integer> intArray = new ArrayList<Integer>();
while (scan.hasNextInt())
{
    intArray.add(scan.NextInt());
}

If you need the final results in an array rather then an ArrayLIst, you can use

Integer[] newArray = (Integer[])intArray.toArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.