Answer to Question #13554 in Java | JSP | JSF for Sifsilva

Question #13554
Write a program to protect a computer against unauthorized access using a password. The program assigns a password to a variable.
The user is prompted to enter a password in order to enter the computer system.
If it is the correct password, display a message welcoming the user. If not, display a suitable message rejecting the attempted access.

Improve the program as follows:

Allow the user to enter a password three times. If the password is correct, display a suitable message. If all the attempts including the third attempt are incorrect, display a message rejecting access to the system

Possible algorithm:

1. Ask the password from the user
2. Compare the password
3. if password in incorrect
Display message to enter password again

Improve the algorithm to include all the steps of the solution in the correct order.
Expert's answer
public class Main {
public static void main(String[] args) {
System.out.println("You have three attempts to enter the password.");
private static void comparePass(int attemptsUser) {
Scanner in = new Scanner(;
String pass = new StringC'yourPassword");
String userPass;
int attempts = attemptsUser; int tries;

System.out.println("Please, input password:"); userPass = in.nextO;

if (pass.equals(userPass)) {
System.out.printlnC'Allowed access!");
} else {
attempts += 1; tries = 3 - attempts;

System.out.printlnC'Password incorrect! Left" + tries +" attempts.");

if (attempts < 3) {
} else {
System.out.println("You enter the password incorrectly three times. Access denied!");

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be first!

Leave a comment

Ask Your question

New on Blog