-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordGenerator.java
More file actions
39 lines (36 loc) · 1.5 KB
/
PasswordGenerator.java
File metadata and controls
39 lines (36 loc) · 1.5 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
/* Password Generator with Random Module
* @author : Pradumn Patel (JV Grace)
*/
import java.util.Random;
import java.util.Scanner;
public class PasswordGenerator{
public static String generatePassword(int length,boolean isIncludeNumbers,boolean isIncludeSymbols,boolean isIncludeUpperCase)
{
String numbers=isIncludeNumbers?"0123456789":"";
String lowerCase="abcdefghijklmnopqrstuvwxyz";
String uppercase=isIncludeUpperCase?"ABCDEFGHIJKLMNOPQRSTUVWXYZ":"";
String symbol=isIncludeSymbols?"~!@#$%^&*{}[]?+></":"";
String allChars=numbers+lowerCase+uppercase+symbol;
Random r=new Random();
StringBuilder sbr=new StringBuilder();
for(int i=0;i<length;i++)
{
sbr.append(allChars.charAt(r.nextInt(allChars.length())));
}
return sbr.toString();
}
public static void main(String[] args)
{
try (Scanner sc = new Scanner(System.in)) {
System.out.println("Enter the desired password length: ");
int length=sc.nextInt();
System.out.println("Include number yes/no");
boolean isIncludeNumbers=sc.next().equalsIgnoreCase("yes");
System.out.println("Include symbols yes/no");
boolean isIncludeSymbols=sc.next().equalsIgnoreCase("yes");
System.out.println("Include Uppercase yes/no");
boolean isIncludeUpperCase=sc.next().equalsIgnoreCase("yes");
System.out.println("Generated Password is: "+generatePassword(length,isIncludeNumbers,isIncludeSymbols,isIncludeUpperCase));
}
}
}