Java Sudoku - Not Changing Fields In 2D Array -


i've been running issue. i'm relatively new @ java , trying bite off bit more complex i'm used to. combination of own personal file input , main method cannibalized source other methods. i'm still rusty recursion. reason, assignment command change values in 2d array "board" running through without errors, not changing value. looks kosher me in structure @ least, said, i'm new.

also, i'm looking text output on terminal finished program, , throwing exception seems eyesore on terminal. suggestions?

import java.util.scanner; import java.io.file;  public class sudoku2{      static int board[][] = new int[10][10] ;     static int backtrack = 0;       public static void main(string[] args) throws exception {          sudoku2 mypuzzle = new sudoku2();         //  mypuzzle.readboard();         mypuzzle.readdata("./board/input.txt");         mypuzzle.solve(0, 0);         printboard();      }     protected static void printboard(){         system.out.println("here's puzzle: ");         for(int r = 0; r < 9; r++){             for(int c = 0; c < 9; c++){                 system.out.print(board[r][c]+" ");             }             system.out.println("");         }     }      public void readdata(string filename) {         file inputfile = new file(filename);         try {             scanner keyboard = new scanner(inputfile);             (int row = 0; row < 9; row++) {                 (int col = 0; col < 9; col++) {                      board[row][col] = keyboard.nextint();                 }             }             keyboard.close();         }catch(exception e){             system.out.print("problem in readfile" + e);             e.printstacktrace();         }     }      //check if valid in row     protected static boolean validinrow(int row, int value)     {         for( int col = 0; col < 9; col++ )             if( board[row][col] == value )                 return false ;          return true ;     }      //check if valid in column     protected static boolean validincol(int col, int value)     {         for( int row = 0; row < 9; row++ )             if( board[row][col] == value )                 return false ;          return true ;     }      //check if valid in 3*3     protected static boolean validinblock(int row, int col, int value)     {         row = (row / 3) * 3 ;         col = (col / 3) * 3 ;          for( int r = 0; r < 3; r++ )             for( int c = 0; c < 3; c++ )                 if( board[row+r][col+c] == value )                     return false ;          return true ;     }         //call other methods     public void solve(int row, int col) throws exception     {          if(row > 8)         {             printboard();             throw new exception("solution found") ;         }         else         {              while(board[row][col] != 0)             {                 if( ++col > 8 )                 {                     col = 0 ;                     row++ ;                       if( row > 8 )                         printboard();                     throw new exception( "solution found" ) ;                 }             }               for(int value = 1; value < 10; value++)             {                 if(validinrow(row,value) && validincol(col,value) && validinblock(row,col,value))                 {                      board[row][col] = value;                     //new printevent(board);                        if( col < 8 )                         solve(row, col + 1);                     else                         solve(row + 1, 0);                      backtrack++;                 }             }               board[row][col] = 0;          }     } } 

tenfour04's comment correct. there missing bracket in 1 of if-statements. in solve method, following code:

if ( row > 8 )     printboard(); throw new exception( "solution found" ) ; 

should changed to:

if ( row > 8 ) {     printboard();     throw new exception( "solution found" ) ; } 

in addition, mentioned, misusing exception concept. exception should used handling exceptional, erroneous cases, not printing out terminal.

you can use system.out.println method used in printboard method following:

if ( row > 8 ) {     printboard();     system.out.println( "solution found" ) ;     return; } 

here, added return keyword make program exit solve method when solution found.

hope helps.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -