-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringTask4.java
More file actions
45 lines (32 loc) · 1.92 KB
/
Copy pathStringTask4.java
File metadata and controls
45 lines (32 loc) · 1.92 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
import java.util.Scanner;
public class StringTask4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a password: ");
String enteredPassword = scanner.nextLine();
int passwordLength = enteredPassword.length();
boolean passwordLengthIsValid = passwordLength >= 8 && passwordLength <= 15;
boolean passwordStartsWithLetter = (enteredPassword.charAt(0) >= 'a' && enteredPassword.charAt(0) <= 'z') ||
(enteredPassword.charAt(0) >= 'A' && enteredPassword.charAt(0) <= 'Z');
boolean passwordContainsUppercase = false;
boolean passwordContainsNumber = false;
for (int currentIndex = 0; currentIndex < passwordLength; currentIndex++) {
char currentCharacter = enteredPassword.charAt(currentIndex);
if (currentCharacter >= 'A' && currentCharacter <= 'Z') {
passwordContainsUppercase = true;
}
if (currentCharacter >= '0' && currentCharacter <= '9') {
passwordContainsNumber = true;
}
}
System.out.println("Length between 8 and 15: " + (passwordLengthIsValid ? "PASS" : "FAIL"));
System.out.println("Starts with a letter: " + (passwordStartsWithLetter ? "PASS" : "FAIL"));
System.out.println("Contains uppercase letter: " + (passwordContainsUppercase ? "PASS" : "FAIL"));
System.out.println("Contains a number: " + (passwordContainsNumber ? "PASS" : "FAIL"));
if (passwordLengthIsValid && passwordStartsWithLetter && passwordContainsUppercase && passwordContainsNumber) {
System.out.println("\nPassword is VALID.");
} else {
System.out.println("\nPassword is INVALID.");
}
}
}