0

I am learning how to program in Java. I wrote the following code but, I can't get the if statements to work when the variable inputName is compared to a string and the statement is true. Does anyone have any tips on how to fix this?

import java.util.Scanner;
public class HelloWorld {

  public static void main(String[] args) {
    String x = ("Hello type in your first name.");
    System.out.println(x);
    Scanner keyboard = new Scanner(System.in);
    String inputName = keyboard.next();


    if (inputName == "erick") {
      System.out.println("Hi");
    }
    String v = "victor";
    if (inputName == v) {
      System.out.println("Hi");
    }
    String c = "christy";
    if (inputName == c) {
      System.out.println("Hi");
    }

    keyboard.close();
  }
}
Victor
  • 3
  • 4

1 Answers1

0

don't use == instead use equals() for string comparison.

== will check whether 2 references are referring the same object, while equals() will check the value being referred to.

Ramanlfc
  • 8,283
  • 1
  • 18
  • 24