Create a program [YourName]-Assignment8(replace [YourName] with your actual name
ID: 3759190 • Letter: C
Question
Create a program [YourName]-Assignment8(replace [YourName] with your actual name) that requires users to enter their own password and validates it. The password has to meet the following criteria: 1) The password should be at least eight characters long 2) The password should contain at least one uppercase letter 3) The password should contain at least one lowercase letter 4) The password should have at least one digit 5) The password should have at least one of the following symbols: #, $, &, *, ^, +, -, or @. The program should allow the user to enter a password and then display a message indicating it is valid or not. If the password is not correct the program should ask the user again for a password and validate it repeatedly, until a valid/correct password is provided. ( must be written in Java using text processing and wrapper classes)
Explanation / Answer
import java.util.*;
class PasswordConformity
{
public static void main(String[] args)
{
while(true) {
System.out.println("Enter your password.");
System.out.println("Should contain atleast 8 characters, one Uppercase letter, one lowercase letter");
System.out.println("one digit, at least one of the following symbol ( #, $, &, *, ^, +, -, or @): ");
Scanner in = new Scanner(System.in);
String password = in.next();
String symbol = "#$&*^+-@";
boolean hasLength = false;
boolean hasUpperCase = false;
boolean hasLowerCase = false;
boolean hasDigit = false;
boolean hasSymbol = false;
if(password.length() >= 8)
hasLength = true;
for(int i = 0; i < password.length(); i++)
{
char c = password.charAt(i);
if(Character.isUpperCase(c))
hasUpperCase = true;
if(Character.isLowerCase(c));
hasLowerCase = true;
if(Character.isDigit(c))
hasDigit = true;
if(symbol.indexOf(c) != -1)
hasSymbol = true;
}
if(hasLength && hasUpperCase && hasLowerCase && hasDigit && hasSymbol)
{
System.out.println("The password is Valid.");
return;
}
else
System.out.println("The password is invalid. Try again.");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.