Vmir
Vmir

Reputation: 79

difference between returning true and false statements at the end in a boolean method Java

Good day. I got a slight confusion with the true\false return calls in a boolean method. So the code is :

public class CheckOut {

    public static void main(String[] args) {

        int[][] m = new int[3][3];
        int[][] m1 = new int[m.length][m[0].length];
        System.out.println("Enter the nums for the first matrix : ");
        getM(m);
        System.out.println("Enter the nums for the second matrix : ");
        getM(m1);
        System.out.println(strictlyIdentical(m, m1));

    }

    static int[][] getM(int[][] m) {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < m[i].length; j++) {
                m[i][j] = sc.nextInt();
            }
        }
        return m;
    }

    static boolean strictlyIdentical(int[][] m, int[][] b) {

        if (m.length != b.length && m[0].length != b[0].length) {
            return false;
        }
        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < m[i].length; j++) {
                if (m[i][j] != b[i][j]) {
                    return false;
                }
            }
        }
        return true;
    }
}

The above method works totally fine and returns true if two matrices are identical but
why when I compare the values on their correctness and return true if the vals in the if statements are correct and return false at the end i don't get the desired output .(it is literally true for any entered nums) Consider this:

    static boolean strictlyIdentical(int[][] m, int[][] b) {

        if (m.length == b.length && m[0].length == b[0].length) {
            return true;
        }
        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < m[i].length; j++) {
                if (m[i][j] == b[i][j]) {
                    return true;
                }
            }
        }
        return false;
    }
}


Now I'm comparing the values on their similarity not difference , if i can say so... And the output of this code if given the following input is as follows:

Enter the nums for the first matrix : 
12 3 4 3 2 1 2 3 3
Enter the nums for the second matrix : 
1 2 3 2 3 2 1 2 3 
true

So the preceding method returns true whereas the nums are apparently different. But the logic didn't change in my opinion... is there a certain rule that dictates the order of return statements? or is there a logic issue in my code?

Upvotes: 0

Views: 1586

Answers (4)

lettomobile
lettomobile

Reputation: 316

Your piece of code can't work for many reasons, e.g. why does your function return True if the matrices have the same amount of row and the first rows have the same amount of values?

 if (m.length == b.length && m[0].length == b[0].length) {
            return true;
 }

You don't check the values inside the rows!

By the way I write down my solution, try this and if you have questions I'm here to respond.

    static boolean strictlyIdentical(int[][] m, int[][] b) {

    // Check if both matrices have the same amount of row, if not then return false
    if (m.length == b.length) {
        // If yes save that number
        int rowAmount = m.length;
        // Start control the i-th row
        for (int i = 0; i < rowAmount; i++) {
            // Check if the i-th row of both matrices have the same amount of values, if not then return false
            if(m[i].length != b[i].length) return false;
            // If yes save that number
            int rowLength = m[i].length;
            // Check if values are equal, if not then return false
            for (int j = 0; j < rowLength; j++) {
                if (!(m[i][j] == b[i][j])) return false;
            }
        }
        // Matrices passed all controls, they are IDENTICAL
        return true;
    }
    return false;

Upvotes: 0

JakeTheSnake
JakeTheSnake

Reputation: 402

So im not sure if you just looked at your code too long and didnt see this little line of code, but it always return true because of the first if statement.

    static boolean strictlyIdentical(int[][] m, int[][] b) {

    if (m.length == b.length && m[0].length == b[0].length) {
        return true;
    }
    for (int i = 0; i < m.length; i++) {
        for (int j = 0; j < m[i].length; j++) {
            if (m[i][j] == b[i][j]) {
                return true;
            }
        }
    }
    return false;
}

The first if statement will always return true if the lengths are equal. The example you gave had matrices with the same length, and therefore returned true. EDIT***** Your for statement also will return true on the FIRST match between the matrices. Lookinbg at the if statement, the first index where the 2 matrices are equal, the return causes the code to break out of the function and return true, not considering other cases after the first similarity. After any return statement is called, the function is abandoned and will no longer do any code after the return statement is called.

Upvotes: 2

LastAirBender
LastAirBender

Reputation: 113

When you use return inside a method, it automatically ends the execution of that method. So think about it, at the first two lines you tell your method to return true if only the sizes of the two matrices are identical. So in the example you gave, it's enough that the two matrices are the same size to get true. The same thing happens when you get to the for loops afterward, it is enough to have one identical value at the same index to return true. This is why you should check whether they are not identical, and then return false. And only if they "passed" all examinations, then you can return true.

Upvotes: 0

adq
adq

Reputation: 184

The second implementation definitely has a logic error. The difference is that the first will return false if any two elements are not equal while the second will return true as soon as any two elements are equal, not to mention, the first check is also wrong as JakeTheSnake indicated.

Obviously, in your case, you have to go through every position in both matrices and be sure that each is equal - that's why the the first implementation works, and the second does not.

Upvotes: 0

Related Questions