-3

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:

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)

Now, when I submitted this, the grading program stated that my print(), rowsComplete(), columnsComplete(), and isComplete() methods were incorrect, and that my main() was throwing a java.util.NoSuchElementException. 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.

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<SIZE; i++){
            for (j = 0; j<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 < 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<SIZE; rows++){
        for(int cols = 0; cols<SIZE; cols++){
            if(game[rows][cols]==-1)
                return false;
            temp = game[rows][cols];
            valSeen[temp-1]++;
        }
        for(k=0; k<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<SIZE; cols++){
        for(int rows = 0; rows<SIZE; rows++){
            if(game[rows][cols]==-1)
                return false;
            temp = game[rows][cols];
            valSeen[temp-1]++;
        }
        for(k=0; k<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<SIZE; rows+=3){
        for (int cols=0; cols<SIZE; cols+=3){
            for(int subrows=0; subrows<SUBGRID; subrows++){
                for (int subcols=0; subcols<SUBGRID; subcols++){
                    temp= game[rows+subrows][cols+subcols];
                    if(temp==-1)
                        return false;
                    else
                        valSeen[temp-1]++;
                }
            }
            for(int k=0; k<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) && columnsComplete(valSeen) && subgridsComplete(valSeen))
        return true;
    else
        return false;
}

public String makeHeader()
{
    String header = "   ";
    for (int x = 97; x<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<SIZE; rows++){
        System.out.print(" "+(char)('a'+rows));
        for (int cols=0; cols<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 <row col val> :");
            String rowv = s.next();
            String colv = s.next();
            int valv = s.nextInt();
            puzzle.move(rowv, colv, valv);
        }
    }
}
}
Darth Hunterix
  • 1,484
  • 5
  • 27
  • 31
  • 1
    `at Sudoku.main(Sudoku.java:218)` This is where your error is at. Line `218` of class `Sudoku.java`. Look at this line, see if you can figure out the error. If not... parse your code down to only what's relevant to this line. No one wants to read through 218+ lines of your code. – nhgrif Oct 01 '13 at 01:58
  • I can cut out a few paragraphs of code, but I still believe its due to isComplete() and the other methods I mentioned not functioning correctly. – Kevin Roberts Oct 01 '13 at 02:09

1 Answers1

0

In main method,

Sudoku puzzle = new Sudoku(args[0]);

Your program need an argument to initialize Sudoku which is being taken from user.

String[] args in main is a array of arguments for program which is given as parameters while starting program.

For you program, you'll have to start your Sudoku.class as

java Sudoku argument

You'll have to run you program with argument, else you'll get java.lang.ArrayIndexOutOfBoundsException: 0

TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125