Skip to content
Advertisement

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 1000000 at problem2.main(problem2.java:17)

I get the following error and I don’t know why. I tried looking it up, but I didn’t find a solution.

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 1000000 at problem2.main(problem2.java:17)

Here is my code:

JavaScript

Advertisement

Answer

The problem is not the size of the int[].

Your while loop is constantly checking if a[i] is less than 4000000 while the i variable is already one index ahead. Each loop will have a[i] == 0.

This change will fix the code for you:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement