How to return a boolean value from a method

Return Boolean value from java method

Posted 24 September 2009 - 02:53 PM

I am working on an assignment and I have the code to return a boolean value from a java method, however I am doing something wrong, can someone help

/** NAME: your name
FILE: CSC151 Ch05Ex04.java
NOTE: Any comments that you wish to include.
Ch05Ex04.java. In Ch02Ex07, your wrote a program that generated a business password. The program below asks the user for a password, then calls a method
called getCorrectPassword() to get the business password, then calls a method named isCorrectPassword() to see if the user's input matches the correct password.
A suitable response is generated based on the true or false value returned by this method.
Create the getCorrectPassword() method using the code that you created in Ch02Ex07. This method should create and return the business password the same as
before. Now create the isCorrectPassword() method. This method should receive the user's input, and also the correct password. The method should compare them
and return true if the two passwords are the same, false otherwise.
*/
import javax.swing.JOptionPane;
public class Ch05Ex04
{
/** Obtains a password from the user, tests it, and provides
a suitable response
*/
public static void main (String [] args)
{
String userInput =
JOptionPane.showInputDialog("Please enter your password: ");
String correctPassword = getCorrectPassword();
if (isCorrectPassword(userInput, correctPassword))
JOptionPane.showMessageDialog(null, "Welcome! All doors are opened!");
else
JOptionPane.showMessageDialog(null, "Sorry that passsword is not correct.");
}
/** getCorrectPassword creates the business password
@return The business password
*/
public static String getCorrectPassword()
{
String businessName = "Pump Fitness";
//pwm12p
char letter = businessName.charAt(2);
int stringSize = businessName.length();
char letterOne = businessName.charAt(0);
String password = "pw" + letter + stringSize + letterOne;
String correctPassword = password.toUpperCase();
return correctPassword;
}
/** isCorrectPassword tests a password to see if it correct
@param testP - the password to be tested
@param correctP - the correct password
@return a boolean value true or false
*/
public boolean isCorrectPassword()
{
correctPassword = true;
if(userInput.equals(correctPassword))
return true;
else
return false;
}
}