Hello,
This is my first post on DaniWeb and will probably not be the last so I hope I'm doing this right:
I'm currently working on a Sudoku Solver that allows the user to type in the name of a Sudoku text file that looks like this:
6 8 . . . . . 5 .
. . . . . 5 . . .
. . 3 8 . . 2 6 .
1 . 7 . 2 . . . .
. . 9 5 . 8 6 . .
. . . . 1 . 7 . 2
. 2 1 . . 9 4 . .
. . . 4 . . . . .
. 3 . . . . . 2 8
and then the data will be read in to a 2D array and solved from there. I have tried many different ways to do this from using a Scanner to a BufferReader etc., but the way I have it now that a TA suggested was to read the file from a Scanner, put the file in as a parameter for a Sudoku object and then read the data from there. But everytime I run the program, it runs in to an infinite loop and doesn't do anything.... I wasn't sure if it has something to do with the code I have or where the files are being stored because every other idea I have tried to accomplish this has produced File Not Found errors. I have posted the Sudoku Application class I have (hope these code tags work :) ):
public class SudokuApp
{
public static void main (String[] args) throws IOException
{
Scanner glaDOS = new Scanner (System.in);
System.out.print("PLEASE ENTER A VALID FILE NAME THAT CONTAINS A SUDOKU: ");
File sudokuFile = new File (glaDOS.next());
Sudoku puzzle = new Sudoku (glaDOS);
System.out.println ("Unsolved: ");
System.out.println (puzzle);
System.out.println ("Solved: " );
System.out.println (puzzle);
System.out.println("Backtracking steps: ");
}
And the Sudoku constructor class I currently have that puts the data in to a 2D array:
public Sudoku (Scanner sudokuReader)
{
int[][] puzzle = new int[9][9];
for (row = 0; row < 9; row++)
{
for (col = 0; col < 9; col++)
{
if(sudokuReader.hasNextInt())
{
num = sudokuReader.nextInt();
this.puzzle[row][col] = num;
col++;
if (col == 8)
row++;
}
else
if (row == 8)
return;
}
}
I am really enjoying being part of this community so far, am looking forward to responses and Thank You to all for your help!