0

I'm new to java, and i'm trying to make a array who's size is user defined. I'm staring off by filling in the entire array with zeros and moving from there. How should I go about doing that? What I have right now is something similar to: (input being my scanner object)

int num1, num2, num3 = 0, num4 = 0, active = 0;

num1 = input.nextInt();
num2 = input.nextInt();

int[][] ver = new int[num1][num2];

while(active == 0){

    ver [num3][num4] = 0;
    ++num4;

    if(num4 > num2){

        ++num3;
        num4 = 0;
    }

    if(num3 > num1){

        ++active

    }

}

This keeps giving me an ArrayIndexOutOfBoundsException:0, making me think ver[0][0] doesn't exist. Thanks in advance.

3 Answers3

0

There is no checking for the values of num3 and num4 in your code. Add proper restrictions as I envisage, you don't what them to be greater thatn num1 and num2 respectively.

If the value of num3 or num4 goes beyond the limit you get the ArrayIndexOutOfBoundException.

mtk
  • 13,221
  • 16
  • 72
  • 112
0

You are not checking num3 and num4 properly (they both are allowed to reach the upper bound) so they will eventually go out of bounds.

Since you're just trying to fill your array with zeros why don't you consider the following code:

import java.util.Arrays;

// ...

int num1 = 2; // fixed number just for test purposes
int num2 = 2; // fixed number just for test purposes

int[][] ver = new int[num1][num2];
for (int[] row: ver)
    Arrays.fill(row, 0);

System.out.println(ver[0][0]); // prints 0 as expected

Since every subarray (row in the example) is an array the above code will loop through them filling them with zeros, taking advantage of the fill method of the Arrays class, which you need to import as shown in the example.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • i tried 'for(int[] row: ver){ Arrays.fill(row, 0); } System.out.println(ver[0][0]);' – ILikePotatos Dec 12 '12 at 05:26
  • And what's the result? It should print `num1` times `num2` `0`s – Gabriele Petronella Dec 12 '12 at 05:27
  • it gives me an out of bounds exception on the print statement – ILikePotatos Dec 12 '12 at 05:29
  • that's weird. I just updated my answer with a MWE, which correctly prints 0 as expected. You may want to check you `num1` and `num2` values – Gabriele Petronella Dec 12 '12 at 05:33
  • variables num1 num2 and the array are declared outside an method, because they are edited from a deferent one than brought to another method to fill it with zeros. does this matter? num1 and num2 are static because otherwise it give me an error. I'm not sure what static actually does thought. I made a new class copied you code word by word and its works, so its me not you. – ILikePotatos Dec 12 '12 at 05:49
  • You should then post the missing part of your code that may affect the result, so that we can understand and help you out. – Gabriele Petronella Dec 12 '12 at 05:53
  • heres a link to the code with irrelevant methods hidden: http://pastebin.com/d0C1uZn5 all methods shown: http://pastebin.com/NAg5xp2E – ILikePotatos Dec 12 '12 at 06:01
  • Dont bother; i fixed it myself by giving the method where the zeros when handed out sizeX & Y as args, made a new array and made the zeros do to it, then made the origial array = the new one. Thanks a lot for the Arrays.fill code. it saved me a lot of trouble. thanks for your time too, your where really helpful. – ILikePotatos Dec 12 '12 at 06:07
  • Ok I checked your code and you're initializing the array in the wrong place. Do `behind = new int [sizeX][sizeY];` at the end of your `start()` method and you will be ok. – Gabriele Petronella Dec 12 '12 at 06:08
  • Ok didn't see your last comment. If you initialize the array when declaring the variable, `sizeX` and `sizeY` wont't set yet, so you would be defining a 0x0 array. Moving the initialization after `sizeX` and `sizeY` are set fixes the problem – Gabriele Petronella Dec 12 '12 at 06:10
0

This fails when num4 == num2 as you are checking num4 > num2; because num2 is the length of the array while index goes to length-1. Same goes with num3 and num1 comparison. Use num2-1 and num1-1 in the comparison as below:

    int num1=0, num2=0, num3=0,num4=0;
    num1 = input.nextInt();  //<--Input a value >0
    num2 = input.nextInt();  //<--Input a value >0
    int[][] ver = new int[num1][num2];
    int active = 0;
    while(active == 0){
        ver[num3][num4] = 0;
        ++num4;
        if(num4 > num2-1){     // or if(num4 >= num2)
            ++num3;
            num4 = 0;
        }
        if(num3 > num1-1){     // or if(num3 >= num1)
            ++active;
        }
    }
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73