Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By penguinCoder

Thread: Help with Boolean Function

  1. #1
    rbalagon is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Help with Boolean Function

    Hi guys,

    Would anybody be able to tell me why when this Boolean function is called is always returning false even if I type any of the strings below using the nextLine() from Scanner class?

    y
    Y
    yes
    Yes
    YES

    Here is the code for the function:

    Java Code:
    public static Boolean funcIsThisCorrect(String answer)
    	{
    		
    		
    		if (answer == "y" || answer == "Y" || answer == "yes" || answer == "Yes" || answer == "YES")
    		
    			return true;
    		
    		else
    			
    			return false;
    			
    		
    	}
    Here is the code for where this function is being used

    Java Code:
    public static void main(String[] args) 
    	{
    		String IsThisCorrect;
    		
    		GetTableType();
    		System.out.print("You would like to create a " + tableLimit );
    		System.out.print(" by " + tableLimit + " " + TableTypeToString(tableType) + " table.");
    		System.out.println(" Is this correct? (Y/N)");
    		IsThisCorrect = scanString.nextLine();
    		System.out.println(IsThisCorrect);
    		
    		//
    		while (funcIsThisCorrect(IsThisCorrect) == false)
    		{
    			System.out.println(funcIsThisCorrect(IsThisCorrect));
    			GetTableType();
    			System.out.print("You would like to create a " + tableLimit );
    			System.out.print(" by " + tableLimit + " " + TableTypeToString(tableType) + " table.");
    			System.out.println(" Is this correct? (Y/N)");
    			IsThisCorrect = scanString.nextLine();
    			
    		}

    Any help would be greatly appreciated.

    Russ
    Last edited by rbalagon; 10-11-2012 at 01:14 AM. Reason: Included the method that calls the function in question.

  2. #2
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    175
    Rep Power
    2

    Default Re: Help with Boolean Function

    if you give me the full code I can put it in my computer and trouble shoot if for you to find the errors if you want

  3. #3
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default Re: Help with Boolean Function

    You can cut down on your logic by using the toLowerCase() method.

    Java Code:
    if( answer.toLowerCase() == "y" || answer.toLowerCase() == "yes" )
    This will accomplish the same thing as your logic, but will be a lot less complicated to look at. Just a FYI thing..

    On to your problem, you cannot compare a string using '=='. You must use the .equals() method to compare a string.

    Java Code:
    if( answer.toLowerCase().equals("y") || answer.toLowerCase().equals("yes") )
    Fubarable likes this.

  4. #4
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    175
    Rep Power
    2

    Default Re: Help with Boolean Function

    Im sorry i dont know if i will be able to fix this with my limited knowledge

  5. #5
    rbalagon is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Re: Help with Boolean Function

    Thanks for the replies.

    @penguincoder

    Your solution is what I was look for. Thank you.

  6. #6
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default Re: Help with Boolean Function

    You're welcome kind sir. :)

Similar Threads

  1. Recursive function to iteractive function
    By mikeZet in forum New To Java
    Replies: 0
    Last Post: 03-13-2012, 01:42 AM
  2. Replies: 3
    Last Post: 09-13-2011, 12:01 AM
  3. boolean error help when no boolean is given
    By drewtrcy in forum New To Java
    Replies: 18
    Last Post: 05-05-2011, 09:04 AM
  4. Replies: 10
    Last Post: 04-06-2011, 04:45 PM
  5. Replies: 3
    Last Post: 12-04-2009, 03:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •