Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Trouble using || OR operator (Java)

  • 10-11-2012 10:42PM
    #1
    Registered Users, Registered Users 2 Posts: 731 ✭✭✭


    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: 45,151 ✭✭✭✭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: 731 ✭✭✭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: 731 ✭✭✭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: 731 ✭✭✭Morpork


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


Advertisement