Results 1 to 7 of 7
  1. #1
    Christa912 is offline Member
    Join Date
    Sep 2013
    Posts
    9
    Rep Power
    0

    Default while loop not working

    while( !word.equals("q") || !word.equals("Q") )

    The program needs to except both lowercase and uppercase,but it doesn't work if I add the uppercase or statement. It works with just !word.equals("q"). I've tried word.LowerCase() but it doesnt change it.

    Help!

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,063
    Rep Power
    8

    Default Re: while loop not working

    There is an equalsIgnoreCase() you know.

    When in doubt, always start by checking out the API documentation.

    String (Java Platform SE 7 )

    As for your current code, you want to use &&, not ||. Read it out in English. "It shouldn't be q AND it shouldn't be Q".
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,736
    Rep Power
    7

    Default Re: while loop not working

    I assume you want to continue the loop until either a 'q' or a 'Q' is entered.

    while (!word.equalsIgnoreCase("q")) {

    }

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,736
    Rep Power
    7

    Default Re: while loop not working

    Also, your previous statement:

    Java Code:
    while( !word.equals("q") || !word.equals("Q")  )
    is the same as this:
    Java Code:
    while (!(word.equals("q") && word.equals("Q")))   // see De Morgan's laws
    Since the word can't be equal to "q" and "Q" at the same time it evaluates to false. So ! false == true and the loop never ends.

    Regards,
    Jim

    PS. I was nice; Gimbal2 was helpful
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: while loop not working

    Quote Originally Posted by jim829 View Post
    Since the word can't be equal to "q" and "Q" at the same time it evaluates to false. So ! false == true and the loop never ends.
    s/ends/starts

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,736
    Rep Power
    7

    Default Re: while loop not working

    Well, the following runs forever.

    Java Code:
    public class Snippet {
      
       public static void main(String[] args) {
         String word = "A";
         
         while (!word.equals("q") || !word.equals("Q")) {
            System.out.println("hello");
         }
       }
    
    }
    Regards,
    Jim
    Last edited by jim829; 09-10-2013 at 06:47 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: while loop not working

    hrmph, I'm blind; I missed the '!' and mixed up the '||' and '&&' and I'm without Grolsch and the weather has been lousy here all day ...

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. My while loop is not working, why??
    By esined93 in forum New To Java
    Replies: 3
    Last Post: 10-24-2012, 01:05 AM
  2. for loop not working?
    By Newbieprogrammer in forum New To Java
    Replies: 3
    Last Post: 07-24-2012, 02:55 PM
  3. Loop not working
    By swilliams236 in forum New To Java
    Replies: 2
    Last Post: 11-07-2011, 11:36 PM
  4. for loop not working properly
    By lbgladson in forum New To Java
    Replies: 8
    Last Post: 10-15-2011, 12:33 AM
  5. while loop not working
    By RBNSN83 in forum New To Java
    Replies: 6
    Last Post: 06-21-2010, 07:29 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
  •