Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Condition satisfying but not getting desired Output

  1. #1
    Junior Member
    Join Date
    Jun 2022
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Condition satisfying but not getting desired Output

    I wrote this simple code of logical operators but not getting desired output.
    package logical_operators;
     
    import java.util.Scanner;
     
    public class LogicalOperators {
     
    	public static void main(String[] args) {
    		try(Scanner opr = new Scanner(System.in)){
    			System.out.println("Enter age: ");
    			int age = opr.nextInt();
    			opr.nextLine();
    			System.out.println("Do you have a ticket? ");
    			String ticket = opr.nextLine();
    			if(age >= 18 && ticket == "yes") {
    				System.out.println("You are eligible to take the flight.");
    			} else if(age < 18 || !ticket.equals("yes")) {
    				System.out.println("You are not eligible to take the flight.");
    			}
    		}
    	}
     
    }
    Output that I am getting : Enter age:
    Input: 18
    Do you have a ticket?
    Input: yes
    *****No output*****

    Desired Output: You are eligible to take the flight.
    Last edited by NiharVish; June 26th, 2022 at 12:17 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Condition satisfying but not getting desired Output

    Use the equals method to compare Strings' contents, not the == operator.
    The second comparison of ticket is the right way to do it.

    *****No output*****
    Add an ending else statement after the if statements to catch the case when none of the preceding if statements are true. Print out an appropriate message there about why that ending else was executed.


    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    NiharVish (June 26th, 2022)

  4. #3
    Junior Member
    Join Date
    Jun 2022
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Condition satisfying but not getting desired Output

    Thanks. It's working now. I wonder why can't we use "==" for String comparison !!

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Condition satisfying but not getting desired Output

    why can't we use "==" for String comparison
    == is for comparing the contents of variables, not the contents of objects.
    If two variables refer to the same object, then == will return true.
    The equals method compares the contents of objects.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Looping Array and Satisfying multiple conditions-Support
    By jashvin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 23rd, 2024, 04:46 AM
  2. LSP: weaken pre-condition and strengthen post-condition
    By Capital in forum Object Oriented Programming
    Replies: 1
    Last Post: July 13th, 2014, 12:46 PM
  3. [SOLVED] Code is not giving the desired output
    By Tree_Bek in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2013, 07:27 PM
  4. Replies: 14
    Last Post: March 14th, 2012, 07:17 PM
  5. No Error in Code but Output is not desired...!!!
    By Adi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 28th, 2011, 08:56 AM

Tags for this Thread