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 2 of 2

Thread: "Program execution skips the "if" statement"!

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation "Program execution skips the "if" statement"!

    Hello Everyone,

    I am trying to calculate and print the total cost of movie tickets by asking the user whether he wants to buy a ticket or not.

    In the code I have an output statement which says to enter yes or no. My question tells me even if the user enter Yes in different ways such as Yes,YES,YeS and yEs,

    below is my program which for now takes "Yes" as the user input and carries out the process.

     
    /**
     * Write a description of class Lab3P2 here.
     * Ticket printing
     * @author (Sai) 
     * @version (7May)
     */
    import java.io.*;
    import java.util.Scanner;
    public class Lab3P3
    {
     public static void main(String[] args) throws IOException{ 
          //String choice;
         double a1,c1,sc,s;
         double adult = 15.00;
         double child = 11.00;
         double Scitizen = 9.50;
         double Student = 12.00;
         System.out.println("Movie Tickets\n");
         System.out.println("Adult $15.00\n Child $11.00");
     
         System.out.flush();
     
              Scanner input = new Scanner( System.in );
     
              System.out.println("Do you wish you buy a ticket:(Yes/No)");
              //choice = input.nextLine();
              String choice = input.nextLine();
     
          [COLOR="#FF0000"]if(choice == "Yes")[/COLOR]
             {
                  System.out.println("How Many adult tickets would you wish to buy");
                  a1 = input.nextInt();
                  a1 = a1*adult;
                  System.out.println("How Many Child tickets would you wish to buy");
                  c1 = input.nextInt();
                  c1 = c1*child;
                  System.out.println("How Many SenoirCitizen tickets would you wish to buy");
                  sc = input.nextInt();
                  sc = sc*Scitizen;
                  System.out.println("How Many Student tickets would you wish to buy");
                  s = input.nextInt();
                  s = s*Student;
                  System.out.println("Total cost of Movie Tickets is: $" + (a1+c1+sc+s));
                  System.out.println("Thank you,Have a nice Day\n");
                }
     
                  else            
                  System.out.println("Thank you,Have a nice Day\n");
     
                }
    }
    Does not execute this statement for some reason : if(choice == "Yes")
    The program compiles with no syntax errors but the problem is it skips the highlighted part in the program and jumps to the else statement and prints out the thank you message.

    Can anyone please help me out where I am doing a mistake!

    regards
    Antony


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: "Program execution skips the "if" statement"!

    Hello Antony!
    You should use the String's equals() (or better in your case equalsIgnoreCase()) method instead of the == operator when comparing Strings.

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

    antony1925 (May 7th, 2012)

Similar Threads

  1. Replies: 3
    Last Post: December 7th, 2011, 01:03 AM
  2. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  3. using if statement on string System.getProperty("os.name") not working
    By sauyon in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 29th, 2011, 12:09 PM
  4. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 07:13 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 07:35 AM