197k views
3 votes
write a java program using a do while loop to prompt the user to enter a password. The user should be prompted to enter the password until the correct password "pals" is entered.

User Chrys
by
4.5k points

1 Answer

2 votes
We can import Scanner from Java.utils.Scanner to pull in text entered in the console.

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String password = "";
do {
System.out.print("Enter your password: ");
password = sc.nextLine();
if(!password.equals("pals") {
System.out.println(\\Incorrect Password. Try again.");
}
} while(!password.equals("pals");
User Harlan Kassler
by
4.4k points