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: Why doesn't my if condition work when I compare two strings?

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

    Default Why doesn't my if condition work when I compare two strings?

    Is there a different logic in Java for if statements when it comes to conditions? I mean my attempt to compare a String variable and a String attribute of a class that is on an array of objects was frustrated someway. It will not enter the if block. The two strings are equal. I displayed the values of each strings before the if evaluation and they are equal. The simbol I used was the ==, and I also tried the string.equals(string variable) as well as the
    compareTo() == 0 option but none of those worked. I wish I knew what it is the way to compare two strings. Any advice will be appreciated. Thank you.


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    163
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Why doesn't my if condition work when I compare two strings?

    Having example code would really help to figure out what is going on, but to help you do need to use the equals method when comparing two strings with each other.
    Also note that "asdf" is not equal to "Asdf". They are case-sensitive.

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

    GregBrannon (May 12th, 2014)

Similar Threads

  1. String counter doesn't count the appropriate strings properly
    By PC_flea in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 12th, 2014, 07:57 PM
  2. compare two strings to see if they end with the same sub-string
    By tuathan in forum Loops & Control Statements
    Replies: 2
    Last Post: July 7th, 2012, 07:56 AM
  3. [SOLVED] Assign a loop to variable to compare strings
    By norske_lab in forum Loops & Control Statements
    Replies: 3
    Last Post: March 6th, 2012, 02:46 PM
  4. Why doesn't this work!
    By Alex-Green in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2012, 04:25 AM
  5. Why doesn't this work?
    By mailman in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2012, 11:19 PM