1

I'm currently making a sudoku program, however my current code seems to fail me. The script below should put out a print "Inconsistent sudoku puzzle" if a row contains the same number several times, but sadly it doesn't.. I've tried several different attempts but no succes.

public void checkRow() {
    int count = 0;

    for(int j = 0; j < list.size(); j++) {
        for(int a = 1; a < 10; a++) {
            for (int i=0; i < list.get(j).length(); i++) {
                if(list.get(j).charAt(i) == a) {
                    count++;
                    if(count >= 2) {
                        System.out.println("Inconsistent sudoku puzzle");
                        count = 0;
                    }
                }
            }
            count = 0;
        }
    }
}

This is the collection of all my error checks:

public void errorCheck() {
    this.checkRow();
    this.checkColumn();
    this.checkBox();
}

Here i load it into my main. The code is a lot more elaborate, but these should be the sections involving the issue.

public static void main(String[] args) throws Exception  {
    Sudoku s = new Sudoku("C:\\Users\\caspe\\Downloads\\Sudoku001.sdk");
    s.printBoard();
    s.errorCheck();
    s.getNum();


    while(getNum() > 0) {
        System.out.println("Next move, please (row , column , value )");
        Scanner scanner = new Scanner(System.in);
        int row = scanner.nextInt();
        int column = scanner.nextInt() ;
        int value = scanner.nextInt();
        if (s.moves(row, column, value)); {
            s.errorCheck();
        }

        s.printBoard();
    }
}
0

3 Answers 3

2

The issue

You're using charAt and trying to compare the result of that to a number:

list.get(j).charAt(i) == a 

However doing so you're comparing the ascii value of the character to the number.

Example:

String a = "3";
System.out.println((int) a.charAt(0)); // This prints 51

The solution

If you wanted to compare number values you'd have to do something like this:

String a = "3";
System.out.println(Character.getNumericValue(a.charAt(0))); // This prints 3

Character.getNumericValue(a.charAt(0)) returns the number value of the character.


Implementation

Implementing that into your code would look like this:

Character.getNumericValue(list.get(j).charAt(i)) == a
Sign up to request clarification or add additional context in comments.

Comments

1

This line:

if(list.get(j).charAt(i) == a)

is always false because you compare a char with an int.

Replace it with

if((list.get(j).charAt(i)-'0') == a)

list.get(j).charAt(i)-'0' gives you the numeric representation of the char

Comments

1

the problem is: 'if(list.get(j).charAt(i) == a)'

its comparing with the "a" value on the ascii table

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.