1

I'm new to Java and i decided to experiment, however I'm having hard time trying to understand what is the problem. What my program is supposed to do is to receive login and login2, password and password2, and if they match then program says "You may enter", if not then program says "You may not enter", however, it says it even when login and login2, password and password2 are the same. Any ideas what to do?

public static void main(String args[]) {
    Scanner input = new Scanner (System.in);
    System.out.println("In order to register type your login");
    String login = input.nextLine();
    System.out.println("And password");
    String password = input.nextLine(); 
    System.out.println("Enter Login");
    String login2 = input.nextLine();
    System.out.println("Enter password");
    String password2 = input.nextLine();  
    if (login == login2 && password == password2){
        System.out.println("You may enter");
    }
    else{
        System.out.println("You may not enter");
    }
}
Prabhakaran
  • 169
  • 2
  • 12
Napoleon Sraf
  • 67
  • 1
  • 1
  • 7

1 Answers1

0

A String is an Object in java. You have to compare it with .equals() or .equalsIgnoreCase().

if (login.equals(login2) && password.equals(password2)){
Edd
  • 3,724
  • 3
  • 26
  • 33
Forseth11
  • 1,418
  • 1
  • 12
  • 21