-1

Why it's always login failed even when entered the correct values

import java.util.Scanner;

public class Main 
{
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub


    String usrname = "aaa";
    String usrpass = "aaa";

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter your username :");
    String usrnameInput = sc.nextLine();

    System.out.println("Enter your password :");
    String usrpassInput = sc.nextLine();


    if (usrname  == usrnameInput & usrpass == usrpassInput) {
        System.out.println("Login Successful");
    } else {
        System.out.println("Lofin failed");
    }



}

}
Failed Scientist
  • 1,977
  • 3
  • 29
  • 48
  • You should compare strings via equal method. Do not use == in order to compare strings. – E A Jan 16 '17 at 12:17

1 Answers1

2

try comparing strings using equals

if (usrname.equals(usrnameInput)  && usrpass.equals(usrpassInput)) {
    System.out.println("Login Successful");
} else {
    System.out.println("Lofin failed");
}
Seek Addo
  • 1,871
  • 2
  • 18
  • 30