How to convert a method from boolean to int (java)?

153 Views Asked by At

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;
}
2

There are 2 best solutions below

2
On

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.

public static Pair rowColumnCheck(int[][] array) {

    Pair <Integer, Integer> p = null;

    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)) {
                p = new Pair<Integer, Integer>(i, j);
                return p;
            }
            rowArray[currentNumberRow - 1] = true;
            columnArray[currentNumberColumn - 1] = true;

        }
        // Not really sure why you are doing this?
        for (boolean booleanValue : rowArray) {
            if (!booleanValue) {
                return null;
            }
        }
        for (boolean booleanValue : columnArray) {
            if (!booleanValue) {
                return null;
            }
        }
    }
    return null;
}
0
On

I have a feeling you want sometimes to return a row/column pair, sometimes a row, and sometimes a column. If this is right, then you need to create an extra class:

public class RowCol {
    public final int row;
    public final int col;

    public RowCol(int row, int col) {
        this.row = row;
        this.col = col;
    }
}

Now when you want to identify the place where the error occurred, you can

return new RowCol(i,j);

or to indicate a row without specifying a column

return new RowCol(i,-1);

and similarly for a column

return new RowCol(-1,j);

The return type of your method will be RowCol, and when you get one back from your method, you'll be able to interrogate its row and col fields to find out the co-ordinates of the return value.