0% found this document useful (0 votes)
22 views2 pages

Java Password Generator Code

The document describes a Java program that generates and validates passwords. It takes a user-input password, checks it against validation criteria of length and character types, and outputs the password and a count of each character type if valid.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views2 pages

Java Password Generator Code

The document describes a Java program that generates and validates passwords. It takes a user-input password, checks it against validation criteria of length and character types, and outputs the password and a count of each character type if valid.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Scanner;

public class PasswordGenerator {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.println("Generate your password");


String password = scanner.nextLine();

if (isValidPassword(password)) {
System.out.println("The generated password " + password + " is valid
and has " + countLowerCase(password) + " lowercase alphabet " +
countUpperCase(password) + " uppercase alphabet " +
countSpecialChar(password) + " special character " +
countDigits(password) + " digit");
} else {
System.out.println(password + " is an invalid password");
}

scanner.close();
}

public static boolean isValidPassword(String password) {


if (password.length() < 6 || password.length() > 12) {
return false;
}

boolean hasLowerCase = false;


boolean hasUpperCase = false;
boolean hasSpecialChar = false;

for (char c : password.toCharArray()) {


if (Character.isLowerCase(c)) {
hasLowerCase = true;
} else if (Character.isUpperCase(c)) {
hasUpperCase = true;
} else if (c >= Integer.parseInt("33") && c <= Integer.parseInt("47")
|| c >= Integer.parseInt("58") && c <= Integer.parseInt("62")) {
hasSpecialChar = true;
} else if (!Character.isDigit(c) && !Character.isLetter(c)) {
return false; // Invalid character
}
}

return hasLowerCase && hasUpperCase && hasSpecialChar;


}

public static int countLowerCase(String password) {


int count = 0;
for (char c : password.toCharArray()) {
if (Character.isLowerCase(c)) {
count++;
}
}
return count;
}

public static int countUpperCase(String password) {


int count = 0;
for (char c : password.toCharArray()) {
if (Character.isUpperCase(c)) {
count++;
}
}
return count;
}

public static int countSpecialChar(String password) {


int count = 0;
for (char c : password.toCharArray()) {
if (c == '@' || c == '$' || c == '*' || c == '#') {
count++;
}
}
return count;
}

public static int countDigits(String password) {


int count = 0;
for (char c : password.toCharArray()) {
if (Character.isDigit(c)) {
count++;
}
}
return count;
}
}

You might also like