Click here to Skip to main content
15,393,377 members
Home / Discussions / Java
   

Java

 
AnswerRe: Is truncating a table faster than dropping and re-creating it in SQL? Thanks Azbilegt Chuluunbat Pin
Gerry Schmitz21-Dec-20 7:59
mveGerry Schmitz21-Dec-20 7:59 
QuestionIs there any Java library to invoke SQL queries on SQL Server? Thanks Azbilegt Chuluunbat Pin
Azbilegt Chuluunbat20-Dec-20 5:28
MemberAzbilegt Chuluunbat20-Dec-20 5:28 
AnswerRe: Is there any Java library to invoke SQL queries on SQL Server? Thanks Azbilegt Chuluunbat Pin
Richard MacCutchan20-Dec-20 6:09
mveRichard MacCutchan20-Dec-20 6:09 
QuestionProgramación orientada a objetos en Java Pin
Member 1501835311-Dec-20 2:07
MemberMember 1501835311-Dec-20 2:07 
AnswerRe: Programación orientada a objetos en Java Pin
OriginalGriff11-Dec-20 2:08
mveOriginalGriff11-Dec-20 2:08 
QuestionMy First Java Pin
Nano Tape5-Dec-20 14:58
MemberNano Tape5-Dec-20 14:58 
AnswerRe: My First Java Pin
Richard MacCutchan5-Dec-20 21:31
mveRichard MacCutchan5-Dec-20 21:31 
QuestionDefault Sudoko grid conversion from 9*9 to all grid size Pin
payugaka3-Dec-20 17:22
Memberpayugaka3-Dec-20 17:22 
I have coded for Sudoku puzzle in java.The thing is my code has limitation for giving inputs for 9*9 grid. How do I make my code adaptable for all the grids.Please have patience.I am new to java.
What changes do I need to make so that the code can run on all grid sizes?The grid is square not a rectangle.


Java
class Solution {
    public void solveSudoku(char[][] board) {
        if(solveSudoku2(board)) {
            return;
        }
    }
 
    public boolean solveSudoku2(char[][] board) {
        boolean isEmpty = true;
        int row = -1;
        int col = -1;
        int n = board.length;
 
        //this code is used to check if there exists any empty cell in sudoku board
        //if there is any empty cell, that means we are not done yet and we need to solve it further,
        // so we cannot return true at any point until all the cells are full
        //by empty cell, I mean cells having '.' as the value
        for(int i = 0; i < board.length; i++) {
            for(int j = 0; j < board[0].length; j++) {
               if(board[i][j] == '.') {
                   row = i;
                   col = j;
                   isEmpty = false;
                   break;
               }
            }
            if(!isEmpty) {
                break;
            }
        }
 
        if(isEmpty) {
            return true;
        }
 
        //loop for all the numbers and start placing in the empty cells
        //numbers start from 1 to n
 
        for(int num = 1; num <= n; num++) {
            //convert number to char
            char char_num = (char)(num + '0');
            //check if the number we are adding satisfies all the sudoku rules,
            // if it does, then we place that number in the cell
            if(checkSafe(board,char_num,row,col)) {
                board[row][col] = (char)(num + '0');
 
                //using this number in place row,col, we check for all the other empty places and see if the board is returning true or not
                // if the board is not filled that means that we need to use other number in row,col place.
                //hence backtrack.
                if(solveSudoku2(board)) {
                    return true;
                } else {
                    board[row][col] = '.';
                }
            }
        }
        return false;
    }
 
    public boolean checkSafe(char[][] board, char num, int row, int col) {
        //checkk if num is present in the row
        for(int i = 0; i< board.length; i++ ) {
            if(board[row][i] == num) {
                return false;
            }
        }
 
        for(int j = 0; j < board[0].length; j++) {
            if(board[j][col] == num) {
                return false;
            }
        }
 
        int checknum = (int)Math.sqrt(board.length);
        //check for the current grid. grid will be basically checknum*checknum matrix. where every matrix will start from startrow to  startrow + checknum having checknum length.
        // so, we we have row = 0, then matrix will start from 0 to 2, i.e. the first 3x3 matrix.
        // however, we have row = 2, then also the matrix will start from 0 to 2 - the first 3x3 matrix.
        //however, if row = 3, then we will start our matrix from 3 and cotinute upto 5.
 
        int startrow = row - row % checknum;
        int startcol = col - col % checknum;
        for(int k = startrow; k < startrow + checknum; k++) {
            for(int l = startcol; l < startcol + checknum; l++) {
                if(board[k][l] == num) {
                    return false;
                }   
            }
        }
        return true;
    }
}

AnswerRe: Default Sudoko grid conversion from 9*9 to all grid size Pin
Richard MacCutchan3-Dec-20 22:55
mveRichard MacCutchan3-Dec-20 22:55 
QuestionI'm a beginner and I'm only a month into Java. I struggle with a massive subject, is this a usual thing for begginers? It took me a long time to only understand this code. Pin
Denys Lysenko22-Nov-20 4:09
MemberDenys Lysenko22-Nov-20 4:09 
AnswerRe: I'm a beginner and I'm only a month into Java. I struggle with a massive subject, is this a usual thing for begginers? It took me a long time to only understand this code. Pin
Richard MacCutchan22-Nov-20 4:32
mveRichard MacCutchan22-Nov-20 4:32 
GeneralRe: I'm a beginner and I'm only a month into Java. I struggle with a massive subject, is this a usual thing for begginers? It took me a long time to only understand this code. Pin
Denys Lysenko23-Nov-20 22:38
MemberDenys Lysenko23-Nov-20 22:38 
GeneralRe: I'm a beginner and I'm only a month into Java. I struggle with a massive subject, is this a usual thing for begginers? It took me a long time to only understand this code. Pin
Richard MacCutchan23-Nov-20 22:47
mveRichard MacCutchan23-Nov-20 22:47 
Questionplease help Pin
Member 1499400615-Nov-20 22:33
MemberMember 1499400615-Nov-20 22:33 
AnswerRe: please help Pin
Richard Deeming16-Nov-20 0:22
mveRichard Deeming16-Nov-20 0:22 
AnswerRe: please help Pin
Richard MacCutchan16-Nov-20 0:44
mveRichard MacCutchan16-Nov-20 0:44 
Questionjava applet wach Pin
Member 1499400615-Nov-20 22:31
MemberMember 1499400615-Nov-20 22:31 
AnswerRe: java applet wach Pin
OriginalGriff15-Nov-20 22:32
mveOriginalGriff15-Nov-20 22:32 
QuestionNeed A Project To Show Employers? Ideas? Pin
CoderBryGuy4-Nov-20 7:13
MemberCoderBryGuy4-Nov-20 7:13 
AnswerRe: Need A Project To Show Employers? Ideas? Pin
Richard MacCutchan4-Nov-20 21:41
mveRichard MacCutchan4-Nov-20 21:41 
GeneralRe: Need A Project To Show Employers? Ideas? Pin
Gerry Schmitz5-Nov-20 2:56
mveGerry Schmitz5-Nov-20 2:56 
GeneralRe: Need A Project To Show Employers? Ideas? Pin
Richard MacCutchan5-Nov-20 3:27
mveRichard MacCutchan5-Nov-20 3:27 
QuestionRealm EJB in Wildfly Pin
Andy_Bell31-Oct-20 0:55
MemberAndy_Bell31-Oct-20 0:55 
AnswerRe: Realm EJB in Wildfly Pin
Richard MacCutchan31-Oct-20 1:07
mveRichard MacCutchan31-Oct-20 1:07 
GeneralRe: Realm EJB in Wildfly Pin
Andy_Bell31-Oct-20 9:01
MemberAndy_Bell31-Oct-20 9:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.