Results 1 to 5 of 5
  1. #1
    janey4115 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default 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
    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();
             
             }
    output:
    Please enter classification: f
    Please enter valid classification
    G
    Valid classification:

    However if I try and add an || operator this code no longer works?
    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: ");
    
    
    	}
    
    }
    Output:
    Please enter classification: f
    Please enter valid classification
    G
    Please enter valid classification

    Can anyone please advise why?
    Thanks
    Jane
    Last edited by sunde887; 11-16-2011 at 03:15 PM.

  2. #2
    NigelRen is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default 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( && )?

  3. #3
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default 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

  4. #4
    janey4115 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: OR || not working in while loop when comparing strings

    Thanks NigelRen - yes && fixed the issue - what a silly mistake

  5. #5
    janey4115 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: OR || not working in while loop when comparing strings

    Thanks Petr

Similar Threads

  1. Comparing Strings
    By Adomini in forum New To Java
    Replies: 3
    Last Post: 02-17-2011, 12:20 AM
  2. While loop comparing strings from user
    By N3VRMND in forum New To Java
    Replies: 5
    Last Post: 10-30-2009, 08:18 AM
  3. comparing strings
    By diggitydoggz in forum New To Java
    Replies: 7
    Last Post: 12-23-2008, 04:40 AM
  4. Comparing Strings
    By souFrag in forum Advanced Java
    Replies: 5
    Last Post: 05-21-2008, 09:03 AM
  5. Comparing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:44 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
  •