Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Why won't this while loop work?

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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
    Member Fendaril's Avatar
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    7
    Thanked 6 Times in 5 Posts

    Default Re: Why won't this while loop work?

    Hello trueblue and welcome to the java programming forums. May I see your full code?

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Why won't this while loop work?

    The fault is in your logic... this logic means that it would have to equals "add", "subtract", and "multiply" at the same time for the loop to terminate (impossible). Instead, you should use and logic.
     while(!operatorSelect.equals("add") && !operatorSelect.equals("subtract") && !operatorSelect.equals("multiply"))

Similar Threads

  1. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 04:31 AM
  2. [SOLVED] Trouble with draw and fillRect in pyramid logic using nested loop
    By LiquidMetal in forum Loops & Control Statements
    Replies: 4
    Last Post: April 27th, 2009, 03:25 AM
  3. [SOLVED] Java for loop problem and out put is not coming
    By thewonderdude in forum Loops & Control Statements
    Replies: 9
    Last Post: March 15th, 2009, 02:31 PM