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: Compare two strings

  1. #1
    Junior Member
    Join Date
    Aug 2019
    Location
    Atlanta, Ga
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Compare two strings

    //Example to compare two stings
    boolean equalsOrNot = "This is Jamie".equals("This is jamie");
    boolean equalsOrNot2 = "This is Jamie".equals("Hello World");
    System.out.println(equalsOrNot2);//equalsOrNot2 Print out false

    When I change it to equalsOrNot it should print true However, this is not the case.
    Any Idea what I have done incorrectly?

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

    Default Re: Compare two strings

    equalsOrNot it should print true
    what I have done incorrectly?
    Those two strings do not look to have the same characters. There is a J in one and a j in the other.

    Look at the equalsIgnoreCase method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2019
    Location
    Atlanta, Ga
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compare two strings

    Quote Originally Posted by Norm View Post
    Those two strings do not look to have the same characters. There is a J in one and a j in the other.

    Look at the equalsIgnoreCase method.
    I see my mistake.
    boolean equalsOrNot = "This is Jamie".equals("This is Jamie"); this is correct
    boolean equalsOrNot = "This is Jamie".equals("This is jamie"); this is what I had, and I is false. Jamie and jamie are not the same.

    Thanks

  4. #4
    Junior Member
    Join Date
    Jul 2024
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking Re: Compare two strings

    Hi,

    The issue you're encountering is due to case sensitivity in string comparisons. The equals method in Java compares strings character by character, considering case.

    To make the comparison case-insensitive, you can use equalsIgnoreCase:

    boolean equalsOrNot = "This is Jamie".equalsIgnoreCase("This is jamie");
    System.out.println(equalsOrNot); // This will print true

    `equalsIgnoreCase ignores case differences, so it will return true for strings that are otherwise equal.


    Jack

Similar Threads

  1. [SOLVED] Compare strings
    By amnez1ya in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2022, 05:17 PM
  2. [SOLVED] Compare the last 4 strings in String s1 and String s2
    By javabro in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 27th, 2019, 05:41 AM
  3. Why doesn't my if condition work when I compare two strings?
    By elbiociq in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 12th, 2014, 02:07 AM
  4. 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, 06:56 AM
  5. [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

Tags for this Thread