File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
java/RandomPasswordGenerator Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments