-1

I keep getting the 'good bye' response no matter what I enter, 'yes' or 'no'. How would I change that so responses are correct.

import java.util.Scanner;

public class SimpleQuiz1 {
  public static void main(String[] args){

    Scanner userInputScanner = new Scanner (System.in);

    System.out.println("Are you ready to take this NBA quiz?");
    String answer = userInputScanner.nextLine();

    if(
       answer == ("yes")){
    System.out.println("Then lets get started!!!");

    } else{ 
       answer = ("no");
      System.out.println("Goodbye, come again soon!");
      }
    }
  }
Martin Spamer
  • 5,437
  • 28
  • 44
Marzy
  • 1

1 Answers1

1

Use String.equals(Object obj) rather than == as "==" will check for reference equality whereas .equals compares only the content not the reference.

Scanner userInputScanner = new Scanner(System.in);
    System.out.println("Are you ready to take this NBA quiz?");
    String answer = userInputScanner.nextLine();
    if (answer.equals("yes")) {
        System.out.println("Then lets get started!!!");

    } else {

this should work fine.

Chanda Korat
  • 2,453
  • 2
  • 19
  • 23
Lalitha
  • 11
  • 1
  • This. Do your best to understand to understand this concept, as it applies to all non-primitive objects that extend `Comparable`, like `String`, and will save you many headaches in the future, especially when designing data-structures. – Chris Phillips Oct 26 '17 at 12:53