0

I am trying to scan a string and put it in an if like so: (this is just an example part of the whole program)

Scanner scan=new Scanner();
System.out.print("Enter the word...");
String a=scan.nextLine();
if (a=="Hi")
System.out.println("Hello!")
Else System.exit(0);

So im checking if the user said "Hi" , but it doesnt work like this, I want a way to do it.

I have just started learning java so my questions might seem a little amateur but your answers will help a lot.

1 Answers1

0

Use "Hi".equals(a), == compares identity, not equality.

Also, you should write specifically "Hi".equals(a) and not a.equals("Hi"), because it won't cause a NullPointerException, since a might be null.

Coder-Man
  • 2,391
  • 3
  • 11
  • 19