|
Hello...I'm writing a sudoku project, which can solve a sudoku table with backtracking algortihm...but when I test the value of "matrix" array by debugging I see that these are not correct....can any body help me?
This is an example of input file :
3 0 0 9 0 0 2 0 0
0 0 5 0 0 0 0 0 7
6 0 0 0 3 0 0 0 8
0 0 0 0 6 1 0 4 0
0 4 0 0 0 0 7 0 0
0 3 6 0 0 0 0 9 0
0 2 7 0 0 0 0 0 0
0 0 0 0 1 0 8 0 5
0 9 0 2 0 8 0 0 0
package filesudoku;
import java.io.*;
import java.util.*;
import java.lang.Exception;
public class Sudoku {
public static int[][] matrix;
public static int counter = 0;
public static String matrixView;
public static void main(String[] args) throws FileNotFoundException{
System.out.print("Please enter file name: ");
Scanner in = new Scanner(System.in);
String fileName = in.next();
matrixCreate(fileName);
}
public static void matrixCreate(String fileName) throws FileNotFoundException {
File getFile = new File(fileName);
Scanner scan = new Scanner(getFile);
matrixView = null;
while (scan.hasNextLine()){
matrixView = scan.nextLine();
counter++;
System.out.println(matrixView);
}
scan.close();
matrix = new int[counter] [matrixView.length()];
scan = new Scanner(getFile);
char getCol;
for (int rows = 0; rows < counter; rows++){
matrixView = scan.nextLine();
for (int cols = 0; cols < matrixView.length(); cols++){
getCol = matrixView.charAt(cols);
matrix[rows] [cols] = getCol;
}
}
}
}
|
|
|
|
|
Member 8586072 wrote: but when I test the value of "matrix" array by debugging I see that these are not correct. You need to explain what values you are seeing and where, and why they are incorrect.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Yeah you're right...ok...as you see in the input file , the values in the first line ais like this : 3 0 0 9 0 0 2 0 0
it means that in array (matrix[][]) the values should be like this :
matrix[0][0]=3
matrix[0][1]=0
matrix[0][2]=0
matrix[0][3]=9
.
.
.
and so on...until matrix[8][8]=0...(as we have in input file)
but what I see is like this :
matrix[0][0]=51 !!!!!
matrix[0][1]=32...
matrix[0][2]=48...
and so on until the last one....and the interesting thing is that the size of the matrix that I defined in my code is matrix[9][9]...but when I debug the code it's like that :
matrix[9][17]!!!! I mean the last element of matrix is in matrix[8][16] with "48" for the value...
I hope that I've explained clearly...if it's not obvious tell me to explain more...
|
|
|
|
|
Your counter is being increment by 1 too many because you increment it even when trying to read beyond the last line; you should only increment it when you get a complete line of values. You are setting the number of columns to the length of the final line read in, but this is wrong as there are more characters than actual digits in the line. And finally, you are setting the values in your matrix to each successive character in the input line, rather than tokenising the string into the successive digits and converting them to integers.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Wowww...Thank you Mr.Richard
|
|
|
|