-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordGenerator.java
More file actions
22 lines (22 loc) · 888 Bytes
/
PasswordGenerator.java
File metadata and controls
22 lines (22 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Scanner;
import java.util.Random;
public class PasswordGenerator{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the desired password length: ");
int length=sc.nextInt();
String characters="AQWERTYUIOPLKJHGFDSZXCVBNMasdfghjklpoiuytrewqzxcvbnm0123654789!@#$%^&*()";
String password=generatePassword(length,characters);
System.out.println("Generated password is :"+password);
sc.close();
}
public static String generatePassword(int length,String characters){
Random random=new Random();
StringBuilder password=new StringBuilder();
for(int i=0;i<length;i++){
int index=random.nextInt(characters.length());
password.append(characters.charAt(index));
}
return password.toString();
}
}