0

I'm trying to fill an array with integers using this code:

    int[] steps = new int[1000001];
    steps[0] = 0;
    steps[1] = 1;
    steps[2] = 2;

    for(int i = 1001; i < steps.length; i++){
        if(steps[i]==0){
             steps[i] = steps[i-1]+1;
        }
        int current = i;

        for(int m = current; m > 1; m--){
            int mult = current*m;
            if(mult<steps.length){
                int suma = steps[current]+1;
                if(steps[mult]==0){
                    steps[mult] = suma;
                }
                if(suma<steps[mult]){
                    steps[mult] = suma;
                } 
            }                      
        }
    }

    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();

    for(int k = 0; k < n; k++){

        int q = scan.nextInt();
        System.out.println(steps[q]);

    }

    scan.close();

And I'm getting this:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2147479015 at javaapplication6.JavaApplication6.main(JavaApplication6.java:26) C:\Users\User\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 1 second)

But I can't see why this is happening. I understand that such an exception occurs when you try to access an index that doesn't exist. I've checked many times now my code and I haven't been able to find the issue.

Do you know what does this number mean? Because it is confusing me:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2147479015

NetBeans says the problem is at:

javaapplication6.JavaApplication6.main(JavaApplication6.java:26)

Line 26 is this one:

if(steps[mult]==0)

I think what that is saying is, that at some point, the program is trying to access a nonexistent index of the array. The thing is that I don't understand how that could happen here.

I put this line before to avoid that, but it seemed not to work:

if(mult<steps.length)

Then I modified that line by changing it to this one and it seems to work. It is not showing the exception anymore:

if(mult<steps.length && mult >=0)

Now the problem is that apparently, it does not get into this part of the code:

    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    for(int k = 0; k < n; k++){
        int q = scan.nextInt();
        System.out.println(steps[q]);
    }

    scan.close();

I want to understand first, why that exception is occurring and why it is getting "solved" by adding that other part of the code, and second, why my code seems to get stuck in my first for loop.

I really would appreciate if you could please help me to understand those things.

Thanks in advance.

Reimu
  • 3
  • 2
  • Possible duplicate of [How to debug "java.lang.ArrayIndexOutOfBoundsException" in a Java program?](https://stackoverflow.com/questions/39714788/how-to-debug-java-lang-arrayindexoutofboundsexception-in-a-java-program) – Jacob B. Apr 09 '18 at 00:53
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Ken White Apr 09 '18 at 01:00

1 Answers1

2

In your code you have

int mult = current*m;

where current and m is the size of the steps array

so 1000001 * 1000001 is going to exceed the size of the array and also exceed the max int value

I am not sure what logic you are wanting to do by doing

if(steps[mult]==0){

You problem can be shown with this code

int val = 1000000;

for (int i = 0; i < 100; i++) {
     val = val * val;
     if (val < 0) {
         System.out.println(val);
     }
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • Thank you for answering so quicly. I'll explain myself: I put the next "if" to avoid that problem. if(mult – Reimu Apr 09 '18 at 01:09
  • The problem is that when an int becomes bigegr than Interger.MAX_VALUE, then it overflows and becomes negative, that why you are seeing `-2147479015` – Scary Wombat Apr 09 '18 at 02:46
  • Oh, now I see the problem. Thank you, Scary Wombat :) – Reimu Apr 09 '18 at 04:13