-2

BlueJ keeps writing I have an error in the line

int[] row = arr[i];

and I don't have any other idea how to put the array in the i row of arr into the array row. the problem is in the method specialAll().

public static boolean isIn(int num, int dig) {
    int is;
    boolean bool = false;
    while (num != 0) {
        is = num % 10;
        if (is == dig) {
            bool = true;
        }
        num = num / 10;
    }
    return bool;
}

public static boolean specialArr(int[] arr) {
    int i, dig, num;
    boolean bool = true;
    for (i = 0; i < arr.length - 1; i++) {
        num = arr[i + 1];
        dig = (arr[i]) % 10;
        if (!isIn(num, dig)) {
            bool = false;
        }
    }
    return bool;
}

public static boolean specialAll(int[][] arr) {
    int i;
    boolean bool = true;
    for (i = 0; i < arr[0].length; i++) {
        int[] row = arr[i];
        if (!specialArr(row)) {
            bool = false;
        }
    }
    return bool;
}

when I run the method and input as arr the matrix

{ { 12, 525, 53, 8367, 17, 471 }, { 12, 525, 53, 8365, 152, 22 } }

which is supposed to return true, the program stops in the middle and writes:

`java.lang.ArrayIndexOutOfBoundsException: 2 at Excersise10.specialAll(Excersise10.java:42)

UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
  • are you sure that the error is in this line not the next? I don't see any problems int this code but if you don't have overloaded version of `specialAll` method it will definitely raise an error – m.antkowicz Aug 25 '19 at 05:32
  • I think there is an error in specialArr(row) line and you just show the code of function specialArr() – Vibhanshu Sharma Aug 25 '19 at 05:34
  • when I run the method and input as arr the matrix {{12,525,53,8367, 17, 471},{12,525,53,8365,152,22}}, which is supposed to return true, the program stops in the middle and writes: java.lang.ArrayIndexOutOfBoundsException: 2 at Excersise10.specialAll(Excersise10.java:42) – princessjava Aug 25 '19 at 05:35
  • I would like to reproduce the problem but I can't. I don't know what `specialArr()` is. https://stackoverflow.com/help/minimal-reproducible-example – Pochmurnik Aug 25 '19 at 05:38
  • sure, Ill add it – princessjava Aug 25 '19 at 05:38
  • The error is because you are taking on your matrix you have only 2 rows so when you are asking arr[i] then this tries to take the next row since your loop is on length the rows and not columns, so you get ArrayIndexOutOfBoundsException – Jonathan JOhx Aug 25 '19 at 05:39
  • I'll try, thank you! – princessjava Aug 25 '19 at 05:47

2 Answers2

2

In your for loop, don't put arr[0].length, rather use arr.length. Try with this:

public static boolean specialAll(int[][] arr) {
    int i;
    boolean bool = true;
    for (i = 0; i < arr.length; i++) {
        int[] row = arr[i];
        if (!specialArr(row)) {
            bool = false;
        }
    }
    return bool;
}

The reason is, you are using multi-dimensional array. Your input array, { { 12, 525, 53, 8367, 17, 471 }, { 12, 525, 53, 8365, 152, 22 } } makes the array size like this: int[2][6]; So, here arr.length is 2 and arr[0].length is the second dimension, i.e. 6.

So, now you should understand why you are getting ArrayIndexOutOfBoundsException. Simply because when you are executing this line int[] row = arr[i]; at some point you are passing the value of i greater than 1 whereas in this case the valid one should be less than 2 (arr.length).

UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
1

The error is speaking for itself, ArrayIndexOutOfBoundException it clearly means you are using the index which is not in the array. In such case always look where are you using indexing, for-loop condition and your increment statement.

What you are using as for-loop termination condition is the length of the first element of the multi-dimensional array arr[][], in your case it values are:

{{12,525,53,8367, 17, 471},{12,525,53,8365,152,22}}

Take look at the first array it's length is 6, so the for-loop will go like:

int [] row = arr[0]; // i = 0
int [] row = arr[1]; // i = 1
int [] row = arr[2]; // i = 2, ArrayIndexOutOfBoundException

Solution: Instead of using i < arr[0].length you should use i < arr.legnth,in this way it will never go out of the bound.

Zain Arshad
  • 1,885
  • 1
  • 11
  • 26