Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class PasswordValidator {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String password = scanner.nextLine();
- boolean isValid = false;
- boolean firstRule = passwordLength(password, isValid);
- boolean secondRule = passwordConsistency(password, isValid);
- boolean thirdRule = passwordDigits(password, isValid);
- if (firstRule && secondRule && thirdRule) {
- System.out.println("Password is valid");
- }
- }
- private static boolean passwordLength(String password, boolean isValid) {
- if (password.length() >= 6 && password.length() <= 10) {
- isValid = true;
- } else {
- System.out.println("Password must be between 6 and 10 characters");
- }
- return isValid;
- }
- private static boolean passwordConsistency(String password, boolean isValid) {
- for (int i = 0; i < password.length(); i++) {
- if (!Character.isLetter(password.charAt(i)) && !Character.isDigit(password.charAt(i))) {
- isValid = false;
- System.out.println("Password must consist only of letters and digits");
- break;
- } else {
- isValid = true;
- }
- }
- return isValid;
- }
- private static boolean passwordDigits(String password, boolean isValid) {
- int digitsCounter = 0;
- for (int i = 0; i < password.length(); i++) {
- if (Character.isDigit(password.charAt(i))) {
- digitsCounter++;
- }
- }
- if (digitsCounter >= 2) {
- isValid = true;
- } else {
- System.out.println("Password must have at least 2 digits");
- }
- return isValid;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement