Results 1 to 5 of 5
  1. #1
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default What Will Print? Just Need Verification

    i
    Java Code:
    nt temperature = 78;
    int month = 6;
    String name = "Barnard, KS."
    if (temperature >= 70 && month >=6)
       System.out.println ("Wear bikini\n");
    else if (name == "Barnard, KS.")
       System.out.println ("Nice day to ride my horse\n");
    What is the output of the program when temperature = 70, month = 5, and name = "Barnard, KS."?

    I say the output is Nice day to ride my horse, because the 1st "if" statement is FALSE and falls through to the second "if" statement. Am I right?
    Thanks In Advance

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by lala View Post
    i
    Java Code:
    int temperature = 78;
    int month = 6;
    String name = "Barnard, KS."
    if (temperature >= 70 && month >=6)
       System.out.println ("Wear bikini\n");
    else if (name == "Barnard, KS.")
       System.out.println ("Nice day to ride my horse\n");
    What is the output of the program when temperature = 70, month = 5, and name = "Barnard, KS."?

    I say the output is Nice day to ride my horse, because the 1st "if" statement is FALSE and falls through to the second "if" statement. Am I right?
    Thanks In Advance
    You are right here but not for the reasons you're thinking of, change your code to this:

    Java Code:
    int temperature = 78;
    int month = 5;
    String name = "Barnard, KS."
    if (temperature >= 70 && month >=6)
       System.out.println ("Wear bikini\n");
    else if (name == new String("Barnard, KS."))
       System.out.println ("Nice day to ride my horse\n");
    kind regards,

    Jos

  3. #3
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default What Will Print? Just Need Verification

    Thanks for the quick response. Isn't it true the 1st statement will be FALSE and not execute, and go to the second "else"?
    Why do you say "You are right here but not for the reasons you're thinking of, ...".
    Thanks JosAH

    ~~Carol

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by lala View Post
    Thanks for the quick response. Isn't it true the 1st statement will be FALSE and not execute, and go to the second "else"?
    Why do you say "You are right here but not for the reasons you're thinking of, ...".
    You can't compare Strings using the == operator, use the String.equals( ... ) method instead.

    kind regards,

    Jos

  5. #5
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default What Will Print? Just Need Verification

    Thank you, JosAH.
    lala

Similar Threads

  1. Fingerprint verification
    By venkateshcoolmoon in forum Advanced Java
    Replies: 4
    Last Post: 02-02-2010, 07:19 PM
  2. Finger print scanning and verification
    By kirly in forum Advanced Java
    Replies: 4
    Last Post: 01-29-2010, 02:24 AM
  3. jarsigner verification
    By karan1989 in forum New To Java
    Replies: 1
    Last Post: 11-12-2009, 01:21 PM
  4. Image Verification
    By peiceonly in forum Java Servlet
    Replies: 2
    Last Post: 04-04-2009, 08:38 AM
  5. Input Verification Demo
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 08:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •