-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordValidator.java
More file actions
57 lines (47 loc) · 1.97 KB
/
PasswordValidator.java
File metadata and controls
57 lines (47 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.Scanner;
public class PasswordValidator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("A valid password must have");
System.out.println(" - at least 10 characters");
System.out.println(" - at least one uppercase letter");
System.out.println(" - at least one lowercase letter");
System.out.println(" - at least one number");
System.out.println(" - at least one symbol\n");
System.out.print("Enter your password: ");
String password = scanner.nextLine();
if (isValidPassword(password)) {
System.out.println("Password meets all requirements.");
} else {
System.out.println("A valid password must have");
System.out.println(" - at least 10 characters");
System.out.println(" - at least one uppercase letter");
System.out.println(" - at least one lowercase letter");
System.out.println(" - at least one number");
System.out.println(" - at least one symbol\n");
}
scanner.close();
}
public static boolean isValidPassword(String password) {
if (password.length() < 10) {
return false;
}
boolean hasUppercase = false;
boolean hasLowercase = false;
boolean hasDigit = false;
boolean hasSymbol = false;
for (char c : password.toCharArray()) {
if (Character.isUpperCase(c)) {
hasUppercase = true;
} else if (Character.isLowerCase(c)) {
hasLowercase = true;
} else if (Character.isDigit(c)) {
hasDigit = true;
} else if (!Character.isLetterOrDigit(c)) {
// if the character is not a letter or digit, then it is considered a symbol
hasSymbol = true;
}
}
return hasUppercase && hasLowercase && hasDigit && hasSymbol;
}
}