I'm new to JAVA programming and right now I'm facing a strange case. I set the confirmation variable to the type of String , it will hold the user input. When the user type "yes" The program should create a new user, but it didn't . Instead, it rise the false alarm "Process is canceled by user". It seems the convirmation variable didn't evaluated correctly. Any advice guys. thanks.
This is my code (it's pretty simple though):
import java.util.Scanner;
public class CreateUser {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String firstName;
String lastName;
String confirmation;
System.out.println("Create new user");
System.out.print("Enter first name : ");
firstName = input.nextLine();
System.out.print("Enter last name : ");
lastName = input.nextLine();
System.out.println("Are you sure?");
confirmation = input.nextLine();
System.out.println(confirmation); // this line is to make sure that the variable value is "yes"
if(confirmation == "yes")
{
Users newUser = new Users(firstName, lastName);
System.out.printf("User %s %s has been created\n", firstName, lastName);
System.out.println("Total active users now is :"+Users.getRegisteredUsers());
}
else
{
System.out.println("Process is canceled by user");
}
input.close();
}
}
And this is the output :
Create new user
Enter first name : fin
Enter last name : fin
Are you sure?
yes
yes
Process is canceled by user