How to return a boolean method in java?
boolean, function, java, methods
Solution
You're allowed to have more than one `return` statement, so it's legal to write
if (some_condition) {
return true;
}
return false;
It's also unnecessary to compare boolean values to `true` or `false`, so you can write
if (verifyPwd()) {
// do_task
}
Edit: Sometimes you can't return early because there's more work to be done. In that case you can declare a boolean variable and set it appropriately inside the conditional blocks.
boolean success = true;
if (some_condition) {
// Handle the condition.
success = false;
} else if (some_other_condition) {
// Handle the other condition.
success = false;
}
if (another_condition) {
// Handle the third condition.
}
// Do some more critical things.
return success;
Problem
I need help on how to return a boolean method in java. This is the sample code: ``` public boolean verifyPwd(){ if (!(pword.equals(pwdRetypePwd.getText()))){ txtaError.setEditable(true); txtaError.setText("*Password didn't match!"); txtaError.setForeground(Color.red); txtaError.setEditable(false); } else { addNewUser(); } return //what? } ``` I want the `verifyPwd()` to return a value on either true or false whenever I want to call that method. I want to call that method like this: ``` if (verifyPwd()==true){ //do task } else { //do task } ``` How to set the value for that method?