-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordValidaton.java
More file actions
50 lines (36 loc) · 1.35 KB
/
PasswordValidaton.java
File metadata and controls
50 lines (36 loc) · 1.35 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
package ExceptionHandling;
import java.util.Scanner;
class InvalidPasswordException extends Exception {
public InvalidPasswordException(String message) {
super(message);
}
}
public class PasswordValidaton {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String password;
try {
System.out.print("Enter your password: ");
password = sc.nextLine();
if (password == null) {
throw new NullPointerException("Password is empty");
}
if (!isValidPassword(password)) {
throw new InvalidPasswordException("Password is invalid: Must be at least 8 characters and include a \n" +
"digit.");
}
System.out.println("Password accepted. Registration successful.");
} catch (InvalidPasswordException e) {
System.out.println(e.getMessage());
} catch (NullPointerException e) {
System.out.println("NullPointerException");
}
}
public static boolean isValidPassword(String password) {
if (password.length() < 8) return false;
for (char c : password.toCharArray()) {
if (Character.isDigit(c)) return true;
}
return false;
}
}