Results 1 to 7 of 7
Thread: while loop not working
- 09-10-2013, 06:31 PM #1
Member
- Join Date
- Sep 2013
- Posts
- 9
- Rep Power
- 0
- 09-10-2013, 06:41 PM #2
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 09-10-2013, 06:41 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-10-2013, 06:51 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: while loop not working
Also, your previous statement:
Java Code:while( !word.equals("q") || !word.equals("Q") )
Java Code:while (!(word.equals("q") && word.equals("Q"))) // see De Morgan's laws
Regards,
Jim
PS. I was nice; Gimbal2 was helpfulThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-10-2013, 07:25 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 09-10-2013, 07:40 PM #6
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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"); } } }
JimLast edited by jim829; 09-10-2013 at 07:47 PM.
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-10-2013, 09:17 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Similar Threads
-
My while loop is not working, why??
By esined93 in forum New To JavaReplies: 3Last Post: 10-24-2012, 02:05 AM -
for loop not working?
By Newbieprogrammer in forum New To JavaReplies: 3Last Post: 07-24-2012, 03:55 PM -
Loop not working
By swilliams236 in forum New To JavaReplies: 2Last Post: 11-07-2011, 11:36 PM -
for loop not working properly
By lbgladson in forum New To JavaReplies: 8Last Post: 10-15-2011, 01:33 AM -
while loop not working
By RBNSN83 in forum New To JavaReplies: 6Last Post: 06-21-2010, 08:29 AM
Bookmarks