-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordGenerator.java
More file actions
57 lines (42 loc) · 1.71 KB
/
PasswordGenerator.java
File metadata and controls
57 lines (42 loc) · 1.71 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;
import java.util.Random;
public class PasswordGenerator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String lower = "abcdefghijklmnopqrstuvwxyz";
String numbers = "0123456789";
String symbols = "!@#$%^&*()-_+=<>?";
StringBuilder allCharacters = new StringBuilder();
System.out.print("Enter password length: ");
int length = scanner.nextInt();
System.out.print("Include uppercase letters? (Y/N): ");
if (scanner.next().equalsIgnoreCase("Y")) {
allCharacters.append(upper);
}
System.out.print("Include lowercase letters? (Y/N): ");
if (scanner.next().equalsIgnoreCase("Y")) {
allCharacters.append(lower);
}
System.out.print("Include numbers? (Y/N): ");
if (scanner.next().equalsIgnoreCase("Y")) {
allCharacters.append(numbers);
}
System.out.print("Include symbols? (Y/N): ");
if (scanner.next().equalsIgnoreCase("Y")) {
allCharacters.append(symbols);
}
if (allCharacters.length() == 0) {
System.out.println("You must select at least one option!");
return;
}
StringBuilder password = new StringBuilder();
for (int i = 0; i < length; i++) {
int index = random.nextInt(allCharacters.length());
password.append(allCharacters.charAt(index));
}
System.out.println("Generated Password: " + password);
scanner.close();
}
}