Results 1 to 6 of 6
Thread: Help with Boolean Function
- 10-11-2012, 12:38 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 2
- Rep Power
- 0
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:
Here is the code for where this function is being usedJava Code:public static Boolean funcIsThisCorrect(String answer) { if (answer == "y" || answer == "Y" || answer == "yes" || answer == "Yes" || answer == "YES") return true; else return false; }
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.
RussLast edited by rbalagon; 10-11-2012 at 01:14 AM. Reason: Included the method that calls the function in question.
- 10-11-2012, 12:53 AM #2
Member
- Join Date
- Oct 2012
- Posts
- 31
- Rep Power
- 0
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
- 10-11-2012, 01:18 AM #3
Member
- Join Date
- Oct 2012
- Location
- Tempe, Arizona
- Posts
- 77
- Blog Entries
- 12
- Rep Power
- 0
Re: Help with Boolean Function
You can cut down on your logic by using the toLowerCase() method.
This will accomplish the same thing as your logic, but will be a lot less complicated to look at. Just a FYI thing..Java Code:if( answer.toLowerCase() == "y" || answer.toLowerCase() == "yes" )
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") )
- 10-11-2012, 01:26 AM #4
Member
- Join Date
- Oct 2012
- Posts
- 31
- Rep Power
- 0
Re: Help with Boolean Function
Im sorry i dont know if i will be able to fix this with my limited knowledge
- 10-11-2012, 02:03 AM #5
Member
- Join Date
- Oct 2012
- Posts
- 2
- Rep Power
- 0
Re: Help with Boolean Function
Thanks for the replies.
@penguincoder
Your solution is what I was look for. Thank you.
- 10-11-2012, 02:08 AM #6
Member
- Join Date
- Oct 2012
- Location
- Tempe, Arizona
- Posts
- 77
- Blog Entries
- 12
- Rep Power
- 0
Similar Threads
-
Recursive function to iteractive function
By mikeZet in forum New To JavaReplies: 0Last Post: 03-13-2012, 01:42 AM -
Boolean.True and Boolean.False, why do some people use these?
By Pojahn_M in forum New To JavaReplies: 3Last Post: 09-13-2011, 12:01 AM -
boolean error help when no boolean is given
By drewtrcy in forum New To JavaReplies: 18Last Post: 05-05-2011, 09:04 AM -
Call a PL/SQL function that returns a boolean type using callablestatement.
By renu in forum New To JavaReplies: 10Last Post: 04-06-2011, 04:45 PM -
Possible? Callback function passed as arguments to another function
By TreyAU21 in forum Advanced JavaReplies: 3Last Post: 12-04-2009, 03:08 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks