Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Numbers moving that shouldnt be

1. ## Numbers moving that shouldnt be

This code solves a Sudoku puzzle with no starting values perfectly..
But if i then in some values to start with it moves them around to suit itself which cannot happen.
Can anybody see the problem in the code? because it is frying my brain!

public boolean solve(int row, int col) {

if (row == 9)
{
row = 0;
col++;
}
if (col == 9 )
{
return true;                //Base case - puzzle solved.
}
Object checkHelp = board[row][col];
String s = checkHelp.toString().trim();
if(s.equals('1') ||s.equals('2') ||s.equals('3') ||s.equals('4') ||s.equals('5') ||
s.equals('6') ||s.equals('7') ||s.equals('8') ||s.equals('9') ) // skip filled cells
{

solve(row+1,col);   		    //recursive call
}

for(int val = 1; val <10;val++)
{
if (valid(row,col,val,board))  //checks if value is legal
{
Object input = val;
String z = input.toString().trim();
board[row][col] = z.charAt(0);;
fireTableCellUpdated(row, col);

if (solve(row+1,col))     //call solve starting at next cell
{
return true;
}
}
}

board[row][col] = '0'; // reset on backtrack
return false;

}

2. ## Re: Numbers moving that shouldnt be

t moves them around to suit itself
Try debugging your code by adding printlns to show the values of variables as they are changed by the program. Be sure to print out enough information do show what the code is doing and what are the values of the variables it is using.

3. ## Re: Numbers moving that shouldnt be

Originally Posted by Norm
Try debugging your code by adding printlns to show the values of variables as they are changed by the program. Be sure to print out enough information do show what the code is doing and what are the values of the variables it is using.
Because this is a recursive method debugging with printlns is an extremely difficult thing to do as the console window just gets flooded with printlns, I was hoping someone else on here could possibly see some silly mistake I am making

4. ## Re: Numbers moving that shouldnt be

Sometimes that's what happens. There could be lots of printlns and the answer to your problem will be in those print outs.
If there is one particular content of a square that gets moved, use if statements to control the printlns so that they only print when that content is being set/changed/moved.

5. ## Re: Numbers moving that shouldnt be

Okay.. using printlns this part of my code NEVER executes

Object checkHelp = board[row][col];
String s = checkHelp.toString().trim();
if(s.equals('1') ||s.equals('2') ||s.equals('3') ||s.equals('4') ||s.equals('5') ||
s.equals('6') ||s.equals('7') ||s.equals('8') ||s.equals('9') ) // skip filled cells
{
System.out.println("Skipped Number");
solve(row+1,col); //recursive call
}

anyone?

6. ## Re: Numbers moving that shouldnt be

What statements are before the code you posted that prevents the posted code from executing?
What are the values of the variables that are tested?

Did you test your code? See what this statement does:
System.out.println("1".equals('1')); // false

7. ## Re: Numbers moving that shouldnt be

You are using string comparison to compare chars.

(s.equals('1')) should be (s.equals("1)) etc. Right now, it's false.