Reputation: 1
What should I put as my return statement for the method overall since i'm already using return statements in my if/else?
public static boolean rowsAreValid(int[][] array){
for (int i = 0; i < array.length; i++){
for (int k = i +1; k < array.length; k++){
for (int j = 0; j < array[i].length; j++) {
// Returns false if the numbers are not the same
if (array[i][j] == array[k][j]){
System.out.println("false");
return false;
}
//Returns true if numbers are the same
else {
System.out.println("true");
return true;
}
}
}
}
// RETURN STATEMENT
}
Upvotes: 0
Views: 45
Reputation: 711
Since you want to return true
when the numbers are the same, then put in your return statement: return true
. Because by reasoning, your numbers will never be not the same.
Also as @blm noted on the comments, I think the snippet in your code:
// Returns false if the numbers are not the same
if (array[i][j] == array[k][j]){
...
}
Should be,
// Returns false if the numbers are not the same
if (array[i][j] != array[k][j]){
...
}
Because otherwise, your logic would be incorrect.
Upvotes: 0
Reputation: 4272
This code is confusing, but at the level of why the compiler complains (without addressing the overall structure of the code): You could have a case where the loops don't execute, and if this happens, your if-else block will never execute and you don't have a return. This will happen, for example, if array.length == 0
. You need to decide what the right answer is for this method in those cases and provide a return accordingly.
Upvotes: 2