Skip to content

Commit e72d540

Browse files
authored
Update password_generator_2
1 parent db1ed63 commit e72d540

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

password_generator_2

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
11
package password_generator;
2-
3-
import java.util.Scanner;
4-
import java.security.SecureRandom;
2+
import java.util.Scanner;
3+
import java.security.SecureRandom; // Import the SecrureRandom package
4+
// Here we are using 'SecureRandom' instead of 'Random' for more secure password
55

66
public class password_generator_2 {
77

8-
public String generatePassword(int length) {
8+
// Create an method with a length parameter
9+
public String generatePassword(int length) {
910
String pwd_characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&";
1011
SecureRandom random = new SecureRandom();
12+
13+
// StringBuilder to efficiently build the password character by character
1114
StringBuilder password = new StringBuilder();
1215

1316
for (int i = 0; i < length; i++) {
17+
18+
// Generate an random index from the length of the pwd_characters
1419
char randomChar = pwd_characters.charAt(random.nextInt(pwd_characters.length()));
20+
21+
// append the generated random characters into 'password'
1522
password.append(randomChar);
1623
}
1724

25+
// Returning password as an proper String
1826
return password.toString();
1927
}
2028
public static void main(String[] args) {
29+
30+
// Create an object for an public class
2131
password_generator_2 generator = new password_generator_2();
2232
Scanner sc = new Scanner(System.in);
2333
System.out.println();
24-
System.out.println("Enter the length of the password: ");
34+
System.out.println("Enter the length of the password: "); // Take the
2535
int passwordLength = sc.nextInt();
36+
37+
// Calling an method using object with an Variable
2638
String password = generator.generatePassword(passwordLength);
2739
System.out.println("______________________________________________");
2840
System.out.println();
29-
System.out.println("The Password: " + password);
41+
System.out.println("The Password: " + password); // Lets print the generated password
3042
System.out.println("______________________________________________");
3143
}
3244

0 commit comments

Comments
 (0)