Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello everyone, Could you please check what is wrong with my sudokuVerifier? I cannot seem to make it work. any help would be appreciated. Thank you.

import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;


//tutor's help extensively taken
public class SudokuVerifier {


public static void main(String[] args){


File file = new File("MySudoku.txt");
String cursor="";
String line;

try{
Scanner sudoku = new Scanner(file);
int[][] sudokuArray = new int [9][9];

int position = 0;
boolean check = true;


while (sudoku.hasNextLine()){

line = sudoku.nextLine();
cursor=cursor+line;

System.out.println(line);
}


for (int i = 0; i < cursor.length(); i++){

char character = cursor.charAt(i);
String value = Character.toString(character);
int add = Integer.parseInt(value);
sudokuArray[position][i] = add;
}
position++;

for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
System.out.print(sudokuArray[i][j]);
}
System.out.println();
}
boolean sum1b = true;
for (int i = 0; i < 9; i++){

int sum1 = 0;
for (int j = 0; j < 9; j++){

if ((sudokuArray[i][j] < 1) || (sudokuArray[i][j] > 9)){

sum1b = false;
}
sum1 = sum1 + sudokuArray[i][j];
}
if (sum1 != 45){

sum1b = false;
}
}
for (int i = 0; i < 9; i++){

int sum2 = 0;
for (int j = 0; j < 9; j++){

sum2 = sum2 + sudokuArray[j][i];
}
if (sum2 != 45){

sum1b = false;
}
}
int sum3 = 0;
for (int i = 0; i < 3; i++){

for (int j = 0; j < 3; j++){

sum3 = sum3 + sudokuArray[i][j];
}
}
if (sum3 != 45){

sum1b = false;
}
int sum4 = 0;
for (int i = 3; i < 6; i++){

for (int j = 0; j < 3; j++){

sum4 = sum4 + sudokuArray[i][j];
}
}
if (sum4 != 45){

sum1b = false;
}
int sum5 = 0;
for (int i = 6; i < 9; i++){

for (int j = 0; j < 3; j++){

sum5 = sum5 + sudokuArray[i][j];
}
}
if (sum5 != 45){

sum1b = false;
}
int sum6 = 0;
for (int i = 0; i < 3; i++){

for (int j = 3; j < 6; j++){

sum6 = sum6 + sudokuArray[i][j];
}
}
if (sum6 != 45){

sum1b = false;
}
int sum7 = 0;
for (int i = 3; i < 6; i++){

for (int j = 3; j < 6; j++){

sum7 = sum7 + sudokuArray[i][j];
}
}
if (sum7 != 45){

sum1b = false;
}
int sum8 = 0;
for (int i = 6; i < 9; i++){

for (int j = 3; j < 6; j++){

sum8 = sum8 + sudokuArray[i][j];
}
}
if (sum8 != 45){

sum1b = false;
}
int sum9 = 0;
for (int i = 0; i < 3; i++){

for (int j = 6; j < 9; j++){

sum9 = sum9 + sudokuArray[i][j];
}
}
if (sum9 != 45){

sum1b = false;
}
int sum10 = 0;
for (int i = 3; i < 6; i++){

for (int j = 6; j < 9; j++){

sum10 = sum10 + sudokuArray[i][j];
}
}
if (sum10 != 45){

sum1b = false;
}
int sum11 = 0;
for (int i = 6; i < 9; i++){

for (int j = 6; j < 9; j++){

sum11 = sum11 + sudokuArray[i][j];
}
}
if (sum11 != 45){

sum1b = false;
}
if (sum1b){

System.out.println("Sudoku Verified!!");
}
else{

System.out.println("Invalid Solution.");
}
}

catch (FileNotFoundException e) {
System.out.println("File not found");
}

catch(Exception e){
System.out.println("Something bad happened");
}
finally{
System.out.println("Exiting Program");
}
}

}
Posted
Comments
Sergey Alexandrovich Kryukov 6-Mar-15 23:48pm    
The best help will come from the debugger. You are the one who has the whole project, you should be the one who debug it.
—SA

1 solution

Well one thing is that your cursor variable holds the entire contents of the file (more or less), and you iterate over the sudokuArray using the length of cursor as an indexer, and the array is only 9x9 in size;

C#
int[][] sudokuArray = new int[9][9];

...

for (int i = 0; i < cursor.length(); i++) {
    char character = cursor.charAt(i);
    String value = Character.toString(character);
    int add = Integer.parseInt(value);
    // i is going to be way more than 8 here!
    sudokuArray[position][i] = add;
}

Try printing the exception message instead of "Something bad happened", the exception message will tell you what that bad thing was.

Hope this helps,
Fredrik
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900