Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-23-2009, 01:39 AM
Member
 
Join Date: Mar 2009
Posts: 57
Rep Power: 0
trueblue is on a distinguished road
Default Why won't this while loop work?
Good day,

I am trying to use this method but the literals "add", "subtract" or "divide" are not stopping the while loop from iterating. Why? Is there a better or correct way of doing this? Many thanks.

public String getOperation()
{
String operatorSelect = Dialog.request("Please input add, subtract or multiply");

while(!operatorSelect.equals("add") || !operatorSelect.equals("subtract") || !operatorSelect.equals("multiply"))
{
Dialog.alert("This is not a valid operator, please try again");
operatorSelect = Dialog.request("Please input add, subtract or multiply");
}
return operatorSelect;
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 05-23-2009, 02:05 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,388
Rep Power: 8
Fubarable is on a distinguished road
Default
You may want to use Venn diagrams to help you visualize this type of problem. You'll see that your while condition encompasses all possible conditions -- is always true. Now try drawing the Venn diagram using && instead of ||, and see what comes out.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-23-2009, 05:39 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,449
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Simply read a bit about logical operators in Java. Then you can have a clear idea about the || and && operators, and what's the most suitable for yours.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-23-2009, 12:48 PM
Member
 
Join Date: Mar 2009
Posts: 57
Rep Power: 0
trueblue is on a distinguished road
Default
Thanks for your inputs.

The loop works fine when || is replaced with && or ^ but the statement didn't seem to read right?
OR seemed the right thing to say rather than AND....
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-23-2009, 04:17 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,388
Rep Power: 8
Fubarable is on a distinguished road
Default
Your first statement is equivalent to this:
Code:
int x = getUserInput();
while ((x != 2) || (x != 3)
{
  x = getUserInput();
}
Now tell me when the boolean condition in the while statement will ever be false? Do you see the fallacy here?

If the user enters 2, the second statement is true and because it uses an or operator, the whole statement is true. Same for 3, and same for any number entered. Your logic is the same. Again, learn to use Venn diagrams as most of us understand this better visually. Best of luck.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-23-2009, 08:04 PM
Member
 
Join Date: Mar 2009
Posts: 57
Rep Power: 0
trueblue is on a distinguished road
Default
Ah yes, I see what you're saying - the compound expression will always evaluate to true because if the user inputted int variable equals 2 and returns false for the first part of the compound expression then it will evaluate to true for the second part because 2 does not equal 3.
Many thanks for your help and insightfulness.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-23-2009, 09:10 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,388
Rep Power: 8
Fubarable is on a distinguished road
Default
Glad it helped. Good luck with your projects.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
my loop doesn't work.. pls help??? ashton New To Java 5 01-16-2009 09:24 AM
I need help with my work assig Please tracy-london New To Java 3 01-04-2009 06:50 PM
Just how do I get Java to actually work? MickY G New To Java 5 11-19-2008 04:50 AM
Why doesn't my loop work? d0nmin0 Advanced Java 8 05-26-2008 07:56 PM
how would i get this to work...? deeadeed New To Java 6 12-06-2007 03:58 AM


All times are GMT +2. The time now is 11:59 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org