Results 1 to 7 of 7
  1. #1
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    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.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    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.

  4. #4
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    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....

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your first statement is equivalent to this:
    Java 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.

  6. #6
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    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.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

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

Posting Permissions

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