How to return a boolean method in java?

367,079

Solution 1

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;

Solution 2

try this:

public boolean verifyPwd(){
        if (!(pword.equals(pwdRetypePwd.getText()))){
                  txtaError.setEditable(true);
                  txtaError.setText("*Password didn't match!");
                  txtaError.setForeground(Color.red);
                  txtaError.setEditable(false);
                  return false;
           }
        else {
            return true;
        }
        
}

if (verifyPwd()==true){
    addNewUser();
}
else {
    // passwords do not match

System.out.println("password do not match"); }

Solution 3

You can also do this, for readability's sake

boolean passwordVerified=(pword.equals(pwdRetypePwd.getText());

if(!passwordVerified ){
    txtaError.setEditable(true);
    txtaError.setText("*Password didn't match!");
    txtaError.setForeground(Color.red);
    txtaError.setEditable(false);
}else{
    addNewUser();
}
return passwordVerified;

Solution 4

public boolean verifyPwd(){
        if (!(pword.equals(pwdRetypePwd.getText()))){
                  txtaError.setEditable(true);
                  txtaError.setText("*Password didn't match!");
                  txtaError.setForeground(Color.red);
                  txtaError.setEditable(false);
                  return false;
           }
        else {
            addNewUser();
            return true;
        }
}

Solution 5

Best way would be to declare Boolean variable within the code block and return it at end of code, like this:

public boolean Test(){
    boolean booleanFlag= true; 
    if (A>B)
    {booleanFlag= true;}
    else 
    {booleanFlag = false;}
    return booleanFlag;

}

I find this the best way.

Share:
367,079
Jay Marz
Author by

Jay Marz

Updated on July 17, 2022

Comments

  • Jay Marz
    Jay Marz almost 2 years

    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?