-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpasswordGenerator.java
More file actions
82 lines (69 loc) · 2.6 KB
/
passwordGenerator.java
File metadata and controls
82 lines (69 loc) · 2.6 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package done;
import java.util.*;
public class passwordGenerator {
private static char upperCase() {
return (char)(65 + (int)(Math.random() * 26));
}
private static char lowerCase() {
return (char)(97 + (int)(Math.random() * 26));
}
private static char digit() {
return (char)(48 + (int)(Math.random() * 10));
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt(); // 65 - 90 // 65 + (int)(Math.random() * 26)
int b = sc.nextInt(); // 97 - 122 // 97 + (int)(Math.random() * 26)
int c = sc.nextInt(); // 48 - 57 // 48 + (int)(Math.random() * 10)
int n = sc.nextInt();
int i;
int key;
int ranVal;
String password = "";
for (i = 0; i < n; i++) {
ranVal = (int)(Math.random() * (n - i));
if (a == 0 && b == 0 & c == 0) {
key = (int)(Math.random() * 3);
switch(key) {
case 0:
password += upperCase();
break;
case 1:
password += lowerCase();
break;
case 2:
password += digit();
break;
}
} else if (ranVal < a) {
password += upperCase();
a--;
} else if (ranVal < a + b) {
password += lowerCase();
b--;
} else {
password += digit();
c--;
}
}
boolean hasMistake = true;
while (hasMistake) {
hasMistake = false;
for (i = 1; i < password.length(); i++) {
if (password.charAt(i - 1) == password.charAt(i)) {
if (Character.isDigit(password.charAt(i))) {
password = password.substring(0, i) + digit() + password.substring(i + 1);
hasMistake = true;
} else if (Character.isUpperCase(password.charAt(i))) {
password = password.substring(0, i) + upperCase() + password.substring(i + 1);
hasMistake = true;
} else {
password = password.substring(0, i) + lowerCase() + password.substring(i + 1);
hasMistake = true;
}
}
}
}
System.out.print(password);
}
}