title: Why do I get an "ArrayIndexOutOfBounds" error in this program?
body: <p>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:</p>
  
  <pre><code>2 5 8 7 1
  3 2 9 4 7
  5 1 2 0 3
  8 0 1 2 7
  </code></pre>
  
  <p>In that table, two appear consecutively, diagonally from the first spot. It can also be like this:</p>
  
  <pre><code>9 5 3 7 0
  2 5 7 3 1
  8 5 0 2 9
  4 5 1 7 5
  </code></pre>
  
  <p>In this table, five appear vertically down from the second spot.</p>
  
  <p>This is the code of the program:</p>
  
  <pre><code>/*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 &lt; firstInput; row++) {
          for (int column = 0; column &lt; 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&lt;randomTable.length; i++){
          for(int t=0; t&lt;randomTable[0].length-3;t++){
          if (randomTable[i][t]==randomTable[i][t+1] &amp;&amp; randomTable[i][t+1]==randomTable[i][t+2] &amp;&amp; randomTable[i][t+2]==randomTable[i][t+3]){
              return true;
          }
      }
  }
  return false;
  }
   public static boolean check_horizontal(int[][] randomTable){
  
       for (int i = 0; i&lt;randomTable.length; i++){
          for(int t=0; t&lt;randomTable[0].length-3;t++){
          if (randomTable[t][i]==randomTable[t+1][i] &amp;&amp; randomTable[t+1][i]==randomTable[t+2][i] &amp;&amp; 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&lt;randomTable.length-3; t++){
      for(int i=0; i&lt;randomTable[0].length-3;i++){
          if (randomTable[i][t]==randomTable[i+1][t+1] &amp;&amp; randomTable[i+1][t+1]==randomTable[i+2][t+2] &amp;&amp; randomTable[i+2][t+2]==randomTable[i+3][t+3]){
              return true;
          }
  
      }
  }
  //check up
  for (int t =0; t&lt;randomTable.length-3; t--){
  for(int i=0; i&lt;randomTable[0].length-3;i++){
      if (randomTable[t][i]==randomTable[t-1][i+1] &amp;&amp; randomTable[t-1][i+1]==randomTable[t-2][i+2] &amp;&amp; randomTable[t-2][i+2]==randomTable[t-3][i+3]){
          return true;
      }
  
  }
  }
  return false;
  }
  }
  </code></pre>
  
  <p>Now the error I get is:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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!</p>