Home > front end >  Java: For Loop iterating strangely causing out of bounds errors
Java: For Loop iterating strangely causing out of bounds errors

Time:03-11

I have a for loop that iterates through a string of numbers. I also have an array that is the same length of the string consisting of numbers that represent that index's max value. The code iterates thru every index of the string and the array and checks to see if number at i in the string is less than or equal to the max value at the index of i in the array.

In this case, the string length is 11, thus the array length is also 11. So, start from 0 up to 10 for the string's and array's indexes. The problem is sometimes the for loop will randomly keep going up to 11 and make the program crash giving an out of bounds error. I say randomly because it can be the first set iterations or up to the 4th set of iterations. I've taken a look at the debugger (Intelli-J IDEA) and it when stepping, i goes up. I don't see why it would.

Code:



for(int i = 0; i < stringOfNumbers.length(); i  ){

    System.out.println(i); // Print for debug purpose
    
    if(Integer.parseInt(String.valueOf(stringOfNumbers.charAt(i))) <= arrayOfMaxValues[i]){

        // do something
        // successful run
    }
    else{
        
        // do something
        // unsuccessful run
    }
}

This is what the console gave me:

0
1
2
3
4
5
6
7
8
9
10
Iteration 1 done
0
1
2
3
4
5
6
7
8
9
10
11
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 11 out of bounds for length 11
    at Main.checkValidity(Main.java:91)
    at Main.main(Main.java:63)

CodePudding user response:

Java resolves 'left to right'. Crucially, the error occurred on arrayOfMaxValues[i], and not stringOfNumbers.charAt(i). If the stringOfNumbers truly had a length of 11, it would have crashed there, with an IndexOutOfBoundsException (and not an ArrayIndexOutOfBoundsException).

Conclusion: Your string is at least 12 characters long.

The code you pasted does not indicate how this happened, but it happened. Your for loop loops from 0 to the length of the string. Thus, if the string is length Y, and your array is length X, where Y is larger than X, this will always happen. Search through your code where you write to the string.

If you write to the string inside the // do something part, you have your explanation. Java doesn't actually start that for loop with: Right, we're looping from 0 to 10, because right now stringOfNumbers.length() is 11.

That's not how it works. Java starts the loop by executing the initializer of the for loop (int i = 0) and will then immediately check the condition (is 0 less than the length of whatever the stringOfNumbers variable is currently pointing at? It is; right now stringOfNumbers.length() is 11). It then loops, and once the loop is done, it executes the incrementor part (i ), and then simply reruns the expression i < stringOfNumbers.length(), and will break out of the loop if the expression is false.

Thus, if you update stringOfNumbers inside the loop, the loop will end later.

If you don't want that, don't write to stringOfNumbers; write to something else and overwrite stringOfNumbers after the loop. Alternatively, you could do:

for (int i = 0, len = stringOfNumbers.length(); i < len; i  ) { ... }

This 'caches' the length when the for loop begins and only checks i against this cached length, not against the length of what stringOfNumbers is currently pointing at.

  • Related