This is a simple Java code that implements a login system. The code has a checkUserInput
method that takes in a username
and password
and checks if it matches with the predefined values for username
and password
. If it matches, it returns true
, otherwise it returns false
.
In the main
method, the code uses a Scanner
to read the input from the user for the username
and password
. Then it calls the checkUserInput
method and based on the returned value, it displays a message indicating whether the login was successful or not.
import java.util.Scanner;
public class LoginExample {
static boolean checkUserInput(String username, String password) {
String pw = "123456", name = "user";
if (username.equals(name) && password.equals(pw)) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String username, password;
System.out.println("Enter username: ");
username = scan.next().toLowerCase();
System.out.println("Enter password: ");
password = scan.next().toLowerCase();
if (checkUserInput(username, password)) {
System.out.print("Successfully logged in");
} else {
System.out.print("Wrong username or password");
}
}
}
Enter username:
user
Enter password:
123456
Successfully logged in
Enter username:
jason
Enter password:
3453
Wrong username or password