Skip to content

Commit 7822a89

Browse files
authored
Create RandomPasswordGenerator.java
1 parent 20e4dd2 commit 7822a89

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.Random;
2+
import java.util.Scanner;
3+
4+
public class RandomPasswordGenerator {
5+
public static String generatePassword(int len) {
6+
//String containing alphanumeric characters i.e [a-z and A-Z and 0-9]
7+
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
8+
//Random class to generate random idex value of the alphanumeric string
9+
Random random = new Random();
10+
//StringBuilder is used to append the random alphanumeric characters
11+
StringBuilder sb = new StringBuilder();
12+
for(int i=0;i<len;i++) {
13+
sb.append(str.charAt(random.nextInt(str.length())));
14+
}
15+
//Converting StringBuilder to String and returning the randomly generated password
16+
return sb.toString();
17+
}
18+
19+
public static void main(String[] args) {
20+
Scanner sc = new Scanner(System.in);
21+
int len;
22+
System.out.println("Enter the length of the password to be generated:");
23+
len = sc.nextInt();
24+
System.out.println(generatePassword(len));
25+
26+
}
27+
28+
}

0 commit comments

Comments
 (0)