Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a boolean method in java?

Tags:

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?

like image 548
Jay Marz Avatar asked Jan 21 '13 03:01

Jay Marz


People also ask

How do I return a boolean from a method in Java?

Java Boolean equals() method The equals() method of Java Boolean class returns a Boolean value. It returns true if the argument is not null and is a Boolean object that represents the same Boolean value as this object, else it returns false.

What is the return type of boolean in Java?

A Boolean expression is a Java expression that returns a Boolean value: true or false .

What is return in boolean method?

Answer: Boolean is a primitive data type in Java that has two return values. A boolean variable can return either “true” or “false”.

How do you get boolean returns?

Just add a return false; outside the for loop so that the method has a return statement in all possible conditions. This means that even if the method execution never finds a match, even then the method should be able to return a boolean value, i.e. false which means it didn't find any match.


2 Answers

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; 
like image 162
Adam Liss Avatar answered Oct 12 '22 23:10

Adam Liss


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"); }

like image 42
PC. Avatar answered Oct 12 '22 22:10

PC.