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

Thread: If Statement Not Working Correctly.

  1. #1
    Junior Member
    Join Date
    Sep 2020
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default If Statement Not Working Correctly.

    If the person replies no to the question on line one, the code is supposed to respond False but it responds True. I don't know what I messed up here, but here is my code, if anyone could help out that would be great:

    System.out.println("Would you like to play again? (yes/no):");
    Scanner findresponse = new Scanner(System.in);
    String response = findresponse.nextLine();
    if (response == "no") {
    System.out.println("False");
    } else {
    System.out.println("True");
    }
    Last edited by stenslae; September 1st, 2020 at 05:49 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,309
    Thanks
    54
    Thanked 2,635 Times in 2,585 Posts

    Default Re: If Statement Not Working Correctly.

    Use the equals() method to compare the contents of String objects, not the == operator.
    If you don't understand my answer, don't ignore it, ask a question.

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

    stenslae (September 1st, 2020)

  4. #3
    Junior Member
    Join Date
    Sep 2020
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: If Statement Not Working Correctly.

    That worked. Thanks!

Similar Threads

  1. Replies: 1
    Last Post: March 18th, 2014, 02:56 AM
  2. Replies: 2
    Last Post: June 25th, 2013, 06:33 AM
  3. My code is not working correctly
    By shavek911 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 18th, 2012, 05:49 PM
  4. [SOLVED] For Loop not working correctly.
    By chrisob in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 15th, 2012, 03:50 PM
  5. Hey guys, my While loop isn't working correctly.
    By metanoia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 26th, 2011, 11:15 PM