-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomPasswordGenerator.java
More file actions
62 lines (46 loc) · 1.72 KB
/
Copy pathRandomPasswordGenerator.java
File metadata and controls
62 lines (46 loc) · 1.72 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
58
59
60
61
62
import java.util.Random;
import java.util.Scanner;
public class RandomPasswordGenerator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random random = new Random();
String lowercase = "abcdefghijklmnopqrstuvwxyz";
String uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String numbers = "0123456789";
String special = "!@#$%^&*()-_=+<>?";
String characters = "";
System.out.print("Enter password length: ");
int length = sc.nextInt();
System.out.print("Include lowercase letters? (yes/no): ");
String lower = sc.next();
System.out.print("Include uppercase letters? (yes/no): ");
String upper = sc.next();
System.out.print("Include numbers? (yes/no): ");
String num = sc.next();
System.out.print("Include special characters? (yes/no): ");
String spec = sc.next();
if (lower.equalsIgnoreCase("yes")) {
characters += lowercase;
}
if (upper.equalsIgnoreCase("yes")) {
characters += uppercase;
}
if (num.equalsIgnoreCase("yes")) {
characters += numbers;
}
if (spec.equalsIgnoreCase("yes")) {
characters += special;
}
if (characters.isEmpty()) {
System.out.println("Error: Select at least one character type.");
} else {
String password = "";
for (int i = 0; i < length; i++) {
int index = random.nextInt(characters.length());
password += characters.charAt(index);
}
System.out.println("\nGenerated Password: " + password);
}
sc.close();
}
}