so I have to build a Sudoku game in Java. I am able confirm in my method that the numbers 1 to 9 only appear once in each row/column. However, I have this set up as a boolean and can not for the life of me figure out how to convert this to an integer (so that I can return the row/column where the error occurs).
public static boolean rowColumnCheck(int[][] array) {
for (int i = 0; i < 9; i++) {
boolean[] rowArray = new boolean[9];
boolean[] columnArray = new boolean[9];
for (int j = 0; j < 9; j++) {
int currentNumberRow = array[i][j];
int currentNumberColumn = array[j][i];
if ((currentNumberRow < 1 || currentNumberRow > 9)
&& (currentNumberColumn < 1 || currentNumberColumn > 9)) {
return false;
}
rowArray[currentNumberRow - 1] = true;
columnArray[currentNumberColumn - 1] = true;
}
for (boolean booleanValue : rowArray) {
if (!booleanValue) {
return false;
}
}
for (boolean booleanValue : columnArray) {
if (!booleanValue) {
return false;
}
}
}
return true;
}
You can't. Each method basically has just a return type and a boolean is not compatible with an Integer. What you could do is change the return type to Integer or to Pair if you need to return a set of coordinates and return null if is not there.
I guess it would be something like this. If null there was no error, and on the pair is the error location.