Return Boolean value from java method

How to return a boolean value from a method

Page 1 of 1

1 Replies - 11356 Views - Last Post: 24 September 2009 - 03:03 PM Rate Topic: -----

#1 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Return Boolean value from java method

Post icon  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;
				
			}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Return Boolean value from java method

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10372
  • View blog
  • Posts: 38,410
  • Joined: 27-December 08

Re: Return Boolean value from java method

Posted 24 September 2009 - 03:03 PM

As it is now, the method isCorrectPassword that accepts 2 parameters hasn't been defined, so the compiler cannot find it. You need to modify your method as such:

public boolean isCorrectPassword(String input, String password){
   return(input.equals(password));
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1