Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Uprajneniq;
- import java.util.Scanner;
- public class PasswordValidator {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String password = scanner.nextLine();
- if (isValidPassword(password)) {
- System.out.println("Password is valid");
- } else {
- if (!isBetween6And10Chars(password)) {
- System.out.println("Password must be between 6 and 10 characters");
- }
- if (!consistOnlyLettersAndDigits(password)) {
- System.out.println("Password must consist only of letters and digits");
- }
- if (!hasAtLeast2Digits(password)) {
- System.out.println("Password must have at least 2 digits");
- }
- }
- scanner.close();
- }
- public static boolean isValidPassword(String password) {
- return isBetween6And10Chars(password) &&
- consistOnlyLettersAndDigits(password) &&
- hasAtLeast2Digits(password);
- }
- public static boolean isBetween6And10Chars(String password) {
- int length = password.length();
- return length >= 6 && length <= 10;
- }
- public static boolean consistOnlyLettersAndDigits(String password) {
- return password.matches("[a-zA-Z0-9]+");
- }
- public static boolean hasAtLeast2Digits(String password) {
- int digitCount = 0;
- for (char c : password.toCharArray()) {
- if (Character.isDigit(c)) {
- digitCount++;
- }
- }
- return digitCount >= 2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement