write a login system shall verify the username and password. If wrong username, please show the error message: Wrong username, please check and the re-enter your username. If wrong password, please show the error message: Wrong password, please check and re-enter your password.
Display message to ask the input from user (1 mark)
User name and password validation ( 2 marks)
Display the error message. (2 marks)
Proper use of loop so the user could re-enter the username/password when wrong input ( 2 marks)
import java.util.Scanner;
public class App {
/**
* The start point of the program
*
* @param args
*
*/
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
final String correctUsername = "Admin";
final String correctPassword = "1111";
String username = "";
do {
System.out.print("Enter username: ");
username = keyBoard.nextLine();
if (username.compareTo(correctUsername) != 0) {
System.out.println("\nWrong username, please check and the re-enter your username.\n");
}
} while (username.compareTo(correctUsername) != 0);
String password = "";
do {
System.out.print("Enter password: ");
password = keyBoard.nextLine();
if (password.compareTo(correctPassword) != 0) {
System.out.println("\nWrong password, please check and the re-enter your password.\n");
}
} while (password.compareTo(correctPassword) != 0);
System.out.print("Successful login.");
keyBoard.close();
}
}
Comments