0

I'm learning the basics of Java, and wanted to practice something. So found a page of some programing problems and I get stuck at this problem:

2) Write a program that asks the user for her name and greets her with her name.

3) Modify the previous program such that only the users Alice and Bob are greeted with their names.

I've made well the 2nd but I have trouble with 3rd one.

System.out.pritnln("Please enter your name ");
Scanner input = user new Scanner(System.in);
String user_name;
user _name = input.next();
if(user_name == "Alice"){
System.out.println("Hello " + user_name + ", sweet name.");
if(user_name=="Bob"){
System.out.println("Hello " + user_name + ", sweet name.");
}
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

Don't use == for string comparison in Java unless you're really sure that is what you are doing. Use:

user_name.equals("Bob")

I actually use equalsIgnoreCase() as my standard approach unless I'm sure it should be case-sensitive.

user2959589
  • 362
  • 1
  • 9
  • 23