Results 1 to 6 of 6
  1. #1
    crafty876 is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default If Statements with string values.

    Hello:

    I am new to Java and am struggling with string values as variables... I have the following small class that just isn't working. My problem is that even when I enter "YES" or "NO", my return response is "Please Enter Yes or No". I suspect I am not using the readLine command correctly and the hard return is causing the problem but not sure.

    Thanks for the help.

    public void run(){
    while(true){
    println(respond(readLine("Would You Like Instructions?")));
    }
    }


    private String respond(String str){
    if(str=="YES"){return "Instructions";}
    else if (str=="NO"){return "Ok";}
    else {return "Please Enter Yes or No";}
    }

  2. #2
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default Re: If Statements with string values.

    "==" compares object references.
    equals() comapres the actual value of the string

    try
    Java Code:
    if(str.equals("YES"))

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: If Statements with string values.

    You should use the equals() method to compare Strings, not the == operator.

  4. #4
    crafty876 is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Re: If Statements with string values.

    Thanks for the help, and the forum

    Best regards,
    Crafty

  5. #5
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default Re: If Statements with string values.

    As a general rule, I only use == for comparing primitive data types (float, int, double, char, boolean, etc.) in Java. The .equals() method is for comparing Objects.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: If Statements with string values.

    Quote Originally Posted by stchman View Post
    As a general rule, I only use == for comparing primitive data types (float, int, double, char, boolean, etc.) in Java. The .equals() method is for comparing Objects.
    Don't forget that enum constants and indeed any static final constants can safely be compared using ==

    There are also use cases where the identity, rather than the semantic equality is of interest.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Returner values from if statements
    By gabrielpr12 in forum New To Java
    Replies: 6
    Last Post: 01-27-2012, 01:03 PM
  2. String If Statements
    By anthropamorphic in forum New To Java
    Replies: 7
    Last Post: 06-22-2011, 03:23 PM
  3. Problems with writing String values to a text file
    By Tedward in forum New To Java
    Replies: 8
    Last Post: 06-02-2011, 04:28 AM
  4. Sorting string Array Values
    By Zerpol in forum New To Java
    Replies: 1
    Last Post: 01-14-2011, 07:08 AM
  5. Passing values to main(String args[]) ?
    By al_Marshy_1981 in forum New To Java
    Replies: 6
    Last Post: 02-21-2010, 10:16 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
  •