forked from tecky708/app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbiswa1ramdmpassword.java
More file actions
58 lines (38 loc) · 2.06 KB
/
biswa1ramdmpassword.java
File metadata and controls
58 lines (38 loc) · 2.06 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
import java.util.Scanner;
import java.util.Random;
public class RandomPasswordGenerator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the desired length of the Password: ");
int length = scanner.nextInt();
System.out.print("Would you like to include numbers? (y/n): ");
boolean includeNumbers = scanner.next().equalsIgnoreCase("y");
System.out.print("Would you like to include lowercase letters? (y/n): ");
boolean includeLowercase = scanner.next().equalsIgnoreCase("y");
System.out.print("Would you like to include uppercase letters? (y/n): ");
boolean includeUppercase = scanner.next().equalsIgnoreCase("y");
System.out.print("Would you like to include special characters? (y/n): ");
boolean includeSpecialChars = scanner.next().equalsIgnoreCase("y");
String password = generateRandomPassword(length, includeNumbers, includeLowercase, includeUppercase, includeSpecialChars);
System.out.println("Your Generated Password is: " + password);
scanner.close();
}
public static String generateRandomPassword(int length, boolean includeNumbers, boolean includeLowercase, boolean includeUppercase, boolean includeSpecialChars) {
String lowercaseChars = "abcdefghijklmnopqrstuvwxyz";
String uppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String numberChars = "0123456789";
String specialChars = "!@#$%^&*()_-+=<>?";
String allChars = "";
if(includeLowercase) allChars += lowercaseChars;
if(includeUppercase) allChars += uppercaseChars;
if(includeNumbers) allChars += numberChars;
if(includeSpecialChars) allChars += specialChars;
Random random = new Random();
StringBuilder password = new StringBuilder();
for(int i = 0; i < length; i++) {
int randomIndex = random.nextInt(allChars.length());
password.append(allChars.charAt(randomIndex));
}
return password.toString();
}
}