Results 1 to 5 of 5
- 11-16-2011, 11:13 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
OR || not working in while loop when comparing strings
Hi - the below code is working as exepected - user enter's invalid classification and is advised - user enters correct classification and is advised
output:Java Code:import java.util.Scanner; public class Hello { public static void main(String[] args) { System.out.print("Please enter classification: "); String classification; Scanner scanner = new Scanner(System.in); classification = scanner.nextLine(); while (!"G".equalsIgnoreCase(classification)) { System.out.println("Please enter valid classification"); classification = scanner.nextLine(); }
Please enter classification: f
Please enter valid classification
G
Valid classification:
However if I try and add an || operator this code no longer works?
Output:Java Code:import java.util.Scanner; public class Hello { public static void main(String[] args) { System.out.print("Please enter classification: "); String classification; Scanner scanner = new Scanner(System.in); classification = scanner.nextLine(); while (!"G".equalsIgnoreCase(classification) || !"PG".equalsIgnoreCase(classification) ) { System.out.println("Please enter valid classification"); classification = scanner.nextLine(); } System.out.println("Valid classification: "); } }
Please enter classification: f
Please enter valid classification
G
Please enter valid classification
Can anyone please advise why?
Thanks
JaneLast edited by sunde887; 11-16-2011 at 03:15 PM.
- 11-16-2011, 11:23 AM #2
Member
- Join Date
- Dec 2010
- Posts
- 4
- Rep Power
- 0
Re: OR || not working in while loop when comparing strings
What your trying to do is ask if the text entered is not "G" or it's not "PG" - which will never be true as it can't be both values. Should you be using an and in there( && )?
- 11-16-2011, 11:27 AM #3
Re: OR || not working in while loop when comparing strings
hi
It does not work because when you enter G the first condition returns false but the second condition returns true and while loop makes a new iteration. You can fix it like this: while (!("G".equals(interUser) || "PG".equals(interUser)))Skype: petrarsentev
http://TrackStudio.com
- 11-16-2011, 11:41 AM #4
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
Re: OR || not working in while loop when comparing strings
Thanks NigelRen - yes && fixed the issue - what a silly mistake
- 11-16-2011, 11:42 AM #5
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Comparing Strings
By Adomini in forum New To JavaReplies: 3Last Post: 02-17-2011, 12:20 AM -
While loop comparing strings from user
By N3VRMND in forum New To JavaReplies: 5Last Post: 10-30-2009, 08:18 AM -
comparing strings
By diggitydoggz in forum New To JavaReplies: 7Last Post: 12-23-2008, 04:40 AM -
Comparing Strings
By souFrag in forum Advanced JavaReplies: 5Last Post: 05-21-2008, 09:03 AM -
Comparing Strings
By Java Tip in forum Java TipReplies: 0Last Post: 12-03-2007, 09:44 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks