Java Sudoku Solving Program: Debugging Help Needed

In summary, there was a mistake in the grid iteration where b[row][col] was used instead of b[k][l] to check for duplicates. This has been fixed and the program now outputs a solved Sudoku board without any repeats in the grids. Additionally, there are suggestions to fix hardcoded lines and use continue statements for more efficient checks.
  • #1
Kaura
122
22
Member warned to post directly in the thread, and give indication of warnings and behavior of program
I am attempting to create a Sudoku Solving Program in Java

This is what I have to far

https://pastebin.com/raw/tgnAGKb2

I have spent close to an hour trying to debug it but to no avail

Can someone more skilled than I please point me in the right direction?
 
Technology news on Phys.org
  • #2
What goes wrong where?
At which step does the program state deviate from your expectations?
 
  • #3
It "solves" the Sudoku Board and outputs the solved board but there are repeats in the grids making the solution incorrect
 
  • #4
Then you should check the function calls that incorrectly claim the assignment would be valid and see what goes wrong there.
 
  • #5
Why not stop it at different points if possible, i.e., run just the first, say 10 lines and see where, at what step, the problem arises?
 
  • #6
Yeah when I get the chance I might just try to debug it at every recursive call
Might take forever but it may be my last hope
 
  • #7
Good luck, if you want help in the Math part, let us know ; unfortunately I am just a beginner programmer, tho others may be able to help you in that regard.
 
  • #8
Java:
for(int k = (row/3)*3; k < (row/3)*3+3; k++) {
           for(int l = (col/3)*3; l < (col/3)*3+3; l++) {
               if(k != row && l != col) {
                   if(b[row][col] == number) return false;
               }
           }
are you sure about that b[row][col] there? if yes, why are you even iterating over k and l? Hopefully this will solve it?
Also some additional stuff you can think about:
1) you could try to fix some lines where you're using hardcoding (such as the line where you check whether your column or row are above the size of the sudoku board). That's the reason you introduced SIZE in your class, no?
2) you could do use a continue statement within the for loops, since you have already checked the same rows and columns.. in other words, if you are in the top left corner [1]
[1][2][3]
[4][5][6]
[7][8][9]
when you reach the grid iteration you have:already check 2,3,4,7... you don't have to recheck them...
if you are at [5], you have already checked 4,6,2,8... etc...
 
Last edited:
  • #9
Code:
//check grids//
        for(int k = (row/3)*3; k < (row/3)*3+3; k++) {
            for(int l = (col/3)*3; l < (col/3)*3+3; l++) {
                if(k != row && l != col) {
                    if(b[row][col] == number) return false;
                }
            }

That && should be an ||. If either (k!= row ) or (l != col) than the (k,l) cell differs from the (row,column) cell and you want to check for a duplicate.
 
  • #10
That && should be an ||. If either (k!= row ) or (l != col) than the (k,l) cell differs from the (row,column) cell and you want to check for a duplicate.

That doesn't matter, because you already checked the columns and rows. The output I get now is:

693857124
124569387
512473896
346791258
435928671
278346519
859214763
781632945
967185432

The problem is clearly with, the 3x3 boxes, since rows and colums have unique numbers.

.
 
  • #11
did you check your grid iteration?
 
  • #12
if(b[row][col] == number) return false;
This should check b[k][l] (the loop variables), not b[row][col] (which is the place we want to validate itself).
 
  • #13
mfb said:
This should check b[k][l] (the loop variables), not b[row][col] (which is the place we want to validate itself).

ChrisVer said:
Java:
for(int k = (row/3)*3; k < (row/3)*3+3; k++) {
           for(int l = (col/3)*3; l < (col/3)*3+3; l++) {
               if(k != row && l != col) {
                   if(b[row][col] == number) return false;
               }
           }
are you sure about that b[row][col] there? if yes, why are you even iterating over k and l? Hopefully this will solve it?
Also some additional stuff you can think about:
1) you could try to fix some lines where you're using hardcoding (such as the line where you check whether your column or row are above the size of the sudoku board). That's the reason you introduced SIZE in your class, no?
2) you could do use a continue statement within the for loops, since you have already checked the same rows and columns.. in other words, if you are in the top left corner [1]
[1][2][3]
[4][5][6]
[7][8][9]
when you reach the grid iteration you have:already check 2,3,4,7... you don't have to recheck them...
if you are at [5], you have already checked 4,6,2,8... etc...

Haha I knew I made some dumb mistake
Thank you so much for finding that
I switched b[row][col] with b[k][l] and the result looks fine now
 
  • Like
Likes mfb

1. What is a Java Sudoku Solving Program?

A Java Sudoku Solving Program is a computer program that uses the Java programming language to solve Sudoku puzzles. It takes in a puzzle as input and uses algorithms and logic to find the solution.

2. How does the debugging process work for a Java Sudoku Solving Program?

The debugging process for a Java Sudoku Solving Program involves identifying and fixing errors or bugs in the code that are preventing the program from working correctly. This can be done by using debugging tools, analyzing the code, and testing the program with different inputs.

3. What are some common errors that can occur in a Java Sudoku Solving Program?

Common errors in a Java Sudoku Solving Program include syntax errors, logic errors, and runtime errors. Syntax errors occur when there is a mistake in the code that violates the rules of the Java programming language. Logic errors occur when the program does not produce the expected output due to a mistake in the logic or algorithm used. Runtime errors occur when the program crashes or stops running due to an unexpected event.

4. How can I troubleshoot and fix errors in my Java Sudoku Solving Program?

To troubleshoot and fix errors in a Java Sudoku Solving Program, you can use debugging tools such as a debugger or print statements to track the flow of the program and identify where the error is occurring. You can also analyze the code and check for any syntax or logic errors. Testing the program with different inputs can also help identify and fix errors.

5. Are there any resources available for help with debugging a Java Sudoku Solving Program?

Yes, there are many resources available for help with debugging a Java Sudoku Solving Program. You can consult online forums and communities, read documentation and tutorials, or seek help from experienced programmers or mentors. Additionally, there are debugging tools and software that can assist in the process.

Similar threads

  • Programming and Computer Science
Replies
2
Views
848
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
9
Views
8K
  • Programming and Computer Science
Replies
1
Views
3K
Back
Top