title: Sudoku generator: ArrayOutOfBoundsException
body: <p>In this project, I am trying to write a program that creates a 9x9 Sudoku board with 9 3x3 subgrids, along with a header row and column that lists the letters a to i. The program compiles correctly, but when I hit run, it gives the following error:</p>
  
  <pre><code>java.lang.ArrayIndexOutOfBoundsException: 0
  at Sudoku.main(Sudoku.java:218)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at `enter code here`edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
  </code></pre>
  
  <p>Now, when I submitted this, the grading program stated that my <code>print()</code>, <code>rowsComplete()</code>, <code>columnsComplete()</code>, and <code>isComplete()</code> methods were incorrect, and that my <code>main()</code> was throwing a <code>java.util.NoSuchElementException</code>. I am confused as to why this is happening. Here is my code for Java, as well as notes on what exactly the methods are supposed to be doing.</p>
  
  <pre><code>import java.util.Scanner;
  import java.io.File;
  import java.io.FileNotFoundException;
  
  public class Sudoku 
  {
  public static final int SIZE = 9;
  public static final int SUBGRID = 3;
  public int[][] game;   
  public int[][] originalGame; 
  public Sudoku(String puzzleFile)
  {
      try
      {
          game = new int[SIZE][SIZE];
          originalGame = new int[SIZE][SIZE];
          File file = new File(puzzleFile);
          Scanner in = new Scanner(file);
          int i = 0;
          int j = 0;
          int k;
          for (i = 0; i&lt;SIZE; i++){
              for (j = 0; j&lt;SIZE; j++){
                  k = in.nextInt();
                  game[i][j]=k;
                  originalGame[i][j] = k;
              }
  
          }
      }
      catch (FileNotFoundException e)
      {
          System.out.println("FileNotFound: " + e.getMessage());
      }   
  }
  
  public void setZero(int[] array)
  {
      int i = 0;
      for (i = 0; i &lt; array.length; i++)
      {
          array[i] = 0;  
      }
  }
  /**
   * This method determines whether the ints 1-9 are present exactly
   * once in each row.  Sets valSeen[i] = 1 if it sees i.  If at any
   * point valSeen[i] is already 1, the rows are not complete because of 
   * duplicate entries.  
   * 
   * If game[x][y] == -1, there is a blank entry so the row cannot be complete.
   * 
   * @param valSeen: an array of ints that serve as flags to indicate whether
   *                 their entry has been seen before or not.
   * 
   * returns: true if each digit 1-9 is present in the row exactly once, else false
   **/
  public boolean rowsComplete(int[] valSeen)
  {     
      int temp;
      int k = 0;
      for(int rows = 0; rows&lt;SIZE; rows++){
          for(int cols = 0; cols&lt;SIZE; cols++){
              if(game[rows][cols]==-1)
                  return false;
              temp = game[rows][cols];
              valSeen[temp-1]++;
          }
          for(k=0; k&lt;valSeen.length; k++){
              if(valSeen[k]!=1)
                  return false;
              else return true;
          }
          setZero(valSeen);
      }
      return true;
  } 
  /**
   * This method determines whether the ints 1-9 are present exactly
   * once in each column.  Sets valSeen[i] = 1 if it sees i.  If at any
   * point valSeen[i] is already 1, the rows are not complete because of 
   * duplicate entries.  
   * 
   * If game[x][y] == -1, there is a blank entry so the row cannot be complete.
   * 
   * @param valSeen: an array of ints that serve as flags to indicate whether
   *                 their entry has been seen before or not.
   * 
   * returns: true if each digit 1-9 is present in the column exactly once, else false
   **/
  public boolean columnsComplete(int[] valSeen)
  {
      int temp;
      int k = 0;
      for(int cols = 0; cols&lt;SIZE; cols++){
          for(int rows = 0; rows&lt;SIZE; rows++){
              if(game[rows][cols]==-1)
                  return false;
              temp = game[rows][cols];
              valSeen[temp-1]++;
          }
          for(k=0; k&lt;valSeen.length; k++){
              if(valSeen[k]!=1)
                  return false;
              else return true;
          }
          setZero(valSeen);
      }
      return true;
  }
  /**
   * This method determines whether the ints 1-9 are present exactly
   * once in each subgrid.  Sets valSeen[i] = 1 if it sees i.  If at any
   * point valSeen[i] is already 1, the rows are not complete because of 
   * duplicate entries.  
   * 
   * If game[x][y] == -1, there is a blank entry so the row cannot be complete.
   * 
   * @param valSeen: an array of ints that serve as flags to indicate whether
   *                 their entry has been seen before or not.
   * 
   * returns: true if each digit 1-9 is present in each subgrid exactly once, else false
   **/   
  public boolean subgridsComplete(int[] valSeen)
  {
      int temp;
      for(int rows=0; rows&lt;SIZE; rows+=3){
          for (int cols=0; cols&lt;SIZE; cols+=3){
              for(int subrows=0; subrows&lt;SUBGRID; subrows++){
                  for (int subcols=0; subcols&lt;SUBGRID; subcols++){
                      temp= game[rows+subrows][cols+subcols];
                      if(temp==-1)
                          return false;
                      else
                          valSeen[temp-1]++;
                  }
              }
              for(int k=0; k&lt;valSeen.length; k++){
                  if(valSeen[k]!=1)
                      return false;
                  else return true;
              }
              setZero(valSeen);
          }
      }
      return true;
  }
  // Create the array valSeen here. I suggest making it = new int[SIZE+1].
  // That way, it will have indexes 0-9, so the ints 1-9 can go into indexes
  // 1-9 instead of mapping them to 0-8 by subtracting 1.
  
  // Call rowsComplete(), columnsComplete(), and subgridsComplete().
  // Be SURE to initialize valSeen to 0 before each method call by using setZero().
  public boolean isComplete()
  {
      int [] valSeen = new int[SIZE+1];
      setZero(valSeen);
      if(rowsComplete(valSeen) &amp;&amp; columnsComplete(valSeen) &amp;&amp; subgridsComplete(valSeen))
          return true;
      else
          return false;
  }
  
  public String makeHeader()
  {
      String header = "   ";
      for (int x = 97; x&lt;106; x++)
          header += ((char)x) + " | " + " ";
      return header;
  }
   /**
   * Prints out the grid.  Each entry has a space to either side, columns are separated by '|'
   * within the grid / between the header and the grid but not externally.  See the specification 
   * for a detailed example.  -1 is replaced with '_'.
   * 
   * Remember that 'a' + 1 = 'b'
   * 
   * Prints the grid to standard out.
   **/
  public void print()
  {
      System.out.println(makeHeader());
      for(int rows=0; rows&lt;SIZE; rows++){
          System.out.print(" "+(char)('a'+rows));
          for (int cols=0; cols&lt;SIZE; cols++){
              if (game[rows][cols]==-1)
                  System.out.print(" | _");
              else
                  System.out.print(" | "+game[rows][cols]);
          }
          System.out.println();
      }
  }
  public void move(String row, String col, int val)
  {
      int rowNumber = ((int)(row.charAt(0)-97));
      int columnNumber = ((int)(col.charAt(0)-97));
      if(originalGame[rowNumber][columnNumber]==-1)
          game[rowNumber][columnNumber]=val;
  }
  
  public static void main(String[] args)
  {
      Sudoku puzzle = new Sudoku(args[0]);
      Scanner s = new Scanner(System.in);
      System.out.println("");
      boolean gameplay = true;
      while (gameplay){
          puzzle.print();
          if(puzzle.isComplete()){
              System.out.println("Puzzle Complete!");
              gameplay=false;
          } else {
              System.out.println("Puzzle Incomplete!");
              System.out.println("Enter new value &lt;row col val&gt; :");
              String rowv = s.next();
              String colv = s.next();
              int valv = s.nextInt();
              puzzle.move(rowv, colv, valv);
          }
      }
  }
  }
  </code></pre>