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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Numbers moving that shouldnt be

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default 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. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Numbers moving that shouldnt be

    Quote Originally Posted by Norm View Post
    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. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default 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. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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. #7
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Stressed
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default 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.

Similar Threads

  1. [METHOD] How: Count how many prime numbers there is between two numbers!
    By Secret20 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 18th, 2011, 02:30 PM
  2. Need help... how to get the line moving?
    By thaiman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 7th, 2010, 10:04 AM
  3. Moving average
    By bondage in forum Loops & Control Statements
    Replies: 0
    Last Post: May 7th, 2010, 04:20 AM
  4. Problem in navigation in a txt file with specific direction
    By wendybird79 in forum Collections and Generics
    Replies: 1
    Last Post: May 10th, 2009, 07:54 AM