Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Trouble using || OR operator (Java)

  • 10-11-2012 9:42pm
    #1
    Registered Users, Registered Users 2 Posts: 728 ✭✭✭


    Hi, just started learning Java and I've ran into a snag.

    The first code works exactly as I want it to.
    (EasyIn. is just replacing the scanner function)
    class single
    {
    	public static void main(String[] args)
    	{
    
    		char ans;
    
    		do
    		{
    			System.out.print("Enter a");
    
    			ans = EasyIn.getChar();
    
    			while (ans != 'a')  
    			{
    				System.out.println("Please enter a");
    				ans = EasyIn.getChar();
    			}
    
    		 }while (ans == 'a');  
    
    	}
    }
    

    This however, doesn't work. It just keeps asking to enter a or b even if I enter them first time or anytime.
    class multiple
    {
    	public static void main(String[] args)
    	{
    
    		char ans;
    
    		do
    		{
    			System.out.print("Enter a");
    
    			ans = EasyIn.getChar();
    
    			while ((ans != 'a') || (ans != 'b'))
    			{
    				System.out.println("Please enter a");
    				ans = EasyIn.getChar();
    			}
    
    		 }while ((ans == 'a') || (ans == 'b'));
    
    	}
    }
    

    Can't figure it out! Can someone please help?
    Thanks.

    Edit: solved, thank you!


Comments

  • Registered Users, Registered Users 2 Posts: 44,194 ✭✭✭✭Basq


    Not sure why you'd need an inner while..
    public static void main(String[] args)
    {
    	char ans;
    
    	do
    	{
    		System.out.print("Please Enter a");
    		ans = EasyIn.getChar();
    	} while ((ans != 'a') || (ans != 'b'));
    }
    

    Would something like that not be sufficient?

    Not fully sure what you're trying to achieve - you're asking for a yet accepting b?


  • Closed Accounts Posts: 5,361 ✭✭✭Boskowski


    if either the left side of the expression is true OR the right is true the entire expression becomes true, thats what OR does, hence you stay in the loop.
    so regardless whether you enter 'a' or 'b' or anything for that matter the expression becomes TRUE and its rinse and repeat, right?
    unless you invent an new character that is 'a' and 'b' at the same time. :o


  • Registered Users, Registered Users 2 Posts: 728 ✭✭✭Morpork


    Thanks for the replies.

    I get what you're saying Boskowski. I wasn't aware of that.
    Sorry for not explaining it well Basq.

    Basically I'm trying to get the user to enter 'Y' or 'y' or 'N' or 'n' as a choice to run the program again or not. And if the users enters anything but those characters it asks to enter one of those characters.

    So I should use the && operator in conjunction with the || operator?
    Maybe I'm over complicating things.

    Also, sorry I forgot to add the "or b" in the second code.


  • Registered Users, Registered Users 2 Posts: 728 ✭✭✭Morpork


    Yes this works perfectly!
    class multiple
    {
    	public static void main(String[] args)
    	{
    
    		char ans;
    
    		do
    		{
    			System.out.print("Enter a or b");
    
    			ans = EasyIn.getChar();
    
    			while ((ans != 'a') && (ans != 'b'))
    			{
    				System.out.println("Please enter a or b");
    				ans = EasyIn.getChar();
    			}
    
    		 }while ((ans == 'a') || (ans == 'b')); 
    
    	}
    }
    


  • Closed Accounts Posts: 5,361 ✭✭✭Boskowski


    sorry my java is not quite fluent but the essence should be correct:
    class multiple {
    public static void main(String[] args) {
       do {
          // do stuff HERE that you may want repeated or not
    
          System.out.print("Repeat? (Y/N)");
          char ans = '';
    
          do { 
             ans = EasyIn.getChar();
          } while ( ans != 'Y' && ans != 'N' )
    // alternatively      } while NOT ( ans == 'Y' || ans == 'N' )
    
       } while ( ans == 'Y' ); 
    }
    


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 728 ✭✭✭Morpork


    Interesting! I'll play around with that. Thank you. :)


Advertisement