Why do I get an "ArrayIndexOutOfBounds" error in this program?

I have written a program in which the user inputs the dimensions of a two-dimensional array, then the computer prints out the table with those dimensions and fills it with random integers from 0 to 9. Then, if there are four consecutive equal integers that appear anywhere in the table, the computer would return "True". If there are no consecutive equal integers in the table, it would return "False". For instance:

2 5 8 7 1
3 2 9 4 7
5 1 2 0 3
8 0 1 2 7

In that table, two appear consecutively, diagonally from the first spot. It can also be like this:

9 5 3 7 0
2 5 7 3 1
8 5 0 2 9
4 5 1 7 5

In this table, five appear vertically down from the second spot.

This is the code of the program:

/*MyName*/
package fourconsecutivenumbers;
import java.util.Random;
import java.util.Scanner;
public class FourConsecutiveNumbers {

public static void main(String[] args) {

    Scanner rowDimension = new Scanner(System.in);
    System.out.print("Enter the number of rows: ");
    int firstInput = rowDimension.nextInt();

    Scanner columnDimension = new Scanner(System.in);
    System.out.print("Enter the number of columns: ");
    int secondInput = columnDimension.nextInt();

    int[][] randomTable = new int[firstInput][secondInput];
    for (int row = 0; row < firstInput; row++) {
        for (int column = 0; column < secondInput; column++) {
            randomTable[row][column] = (int)(Math.random() * 10 + 0);
            System.out.print(randomTable[row][column] + " ");
        }
        System.out.println();
    }
    if(check_horizontal(randomTable) || check_vertical(randomTable) || check_diagonal(randomTable)){
   System.out.println("True");
   }
   else {
    System.out.println("False");
    }
   }


public static boolean check_vertical(int[][] randomTable) {

    for (int i = 0; i<randomTable.length; i++){
        for(int t=0; t<randomTable[0].length-3;t++){
        if (randomTable[i][t]==randomTable[i][t+1] && randomTable[i][t+1]==randomTable[i][t+2] && randomTable[i][t+2]==randomTable[i][t+3]){
            return true;
        }
    }
}
return false;
}
 public static boolean check_horizontal(int[][] randomTable){

     for (int i = 0; i<randomTable.length; i++){
        for(int t=0; t<randomTable[0].length-3;t++){
        if (randomTable[t][i]==randomTable[t+1][i] && randomTable[t+1][i]==randomTable[t+2][i] && randomTable[t+2][i]==randomTable[t+3][i]){
            return true;
        }
    }

}
return false;
}
 public static boolean check_diagonal(int[][] randomTable){
//check down    
for (int t =0; t<randomTable.length-3; t++){
    for(int i=0; i<randomTable[0].length-3;i++){
        if (randomTable[i][t]==randomTable[i+1][t+1] && randomTable[i+1][t+1]==randomTable[i+2][t+2] && randomTable[i+2][t+2]==randomTable[i+3][t+3]){
            return true;
        }

    }
}
//check up
for (int t =0; t<randomTable.length-3; t--){
for(int i=0; i<randomTable[0].length-3;i++){
    if (randomTable[t][i]==randomTable[t-1][i+1] && randomTable[t-1][i+1]==randomTable[t-2][i+2] && randomTable[t-2][i+2]==randomTable[t-3][i+3]){
        return true;
    }

}
}
return false;
}
}

Now the error I get is:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at fourconsecutivenumbers.FourConsecutiveNumbers.check_diagonal(FourConsecutiveNumbers.java:70)
at fourconsecutivenumbers.FourConsecutiveNumbers.main(FourConsecutiveNumbers.java:25)
Java Result: 1

I know that it says the error is at line 70 and then at line 25, but I don't know what I did wrong. I am fairly new to programming so I was hoping that a more experienced program can see what I did wrong. Help is much appreciated!

Jon Skeet
people
quotationmark

This is the problem:

randomTable[t-1][i+1]

When t is 0, that will be accessing randomTable[-1][i + 1] - which can never be valid. Likewise you later go on to t - 3.

Additionally, your loop is starting at 0 but then going backwards... so even if the first iteration completed, you'd end up with t=-1 for the second iteration.

You probably want the loop declaration to be:

for (int t = randomTable.length; t >= 3; t--)

Or just:

for (int t = 3; t < randomTable.length; t++)

depending on whether you really need t to decrease or not.

Additionally, I'd recommend that you follow Java naming conventions - checkHorizontal instead of check_horizontal etc.

people

See more on this question at Stackoverflow