-
Notifications
You must be signed in to change notification settings - Fork 3
/
PasswordGenerator.java
27 lines (23 loc) · 1.09 KB
/
PasswordGenerator.java
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
import java.util.Random;
public class Tester{
public static void main(String[] args) {
System.out.println(generatePassword(8));
}
private static char[] generatePassword(int length) {
String capitalCaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String lowerCaseLetters = "abcdefghijklmnopqrstuvwxyz";
String specialCharacters = "!@#$";
String numbers = "1234567890";
String combinedChars = capitalCaseLetters + lowerCaseLetters + specialCharacters + numbers;
Random random = new Random();
char[] password = new char[length];
password[0] = lowerCaseLetters.charAt(random.nextInt(lowerCaseLetters.length()));
password[1] = capitalCaseLetters.charAt(random.nextInt(capitalCaseLetters.length()));
password[2] = specialCharacters.charAt(random.nextInt(specialCharacters.length()));
password[3] = numbers.charAt(random.nextInt(numbers.length()));
for(int i = 4; i< length ; i++) {
password[i] = combinedChars.charAt(random.nextInt(combinedChars.length()));
}
return password;
}
}