Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Methods;
- import java.util.Scanner;
- public class PasswordValidator_06 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String password = scanner.nextLine();
- //метод, който проверява дали дължината е валидна -> true (валидна), false (невалидна)
- if (!isValidLength(password)) {
- System.out.println("Password must be between 6 and 10 characters");
- }
- //метод, който проверява само букви и цифри -> true (вярно), false (не вярно)
- if (!isValidContent(password)) {
- System.out.println("Password must consist only of letters and digits");
- }
- //метод, който проверява брой на цифрите >= 2 -> true (бр >= 2); false (бр < 2)
- if (!isValidCountDigits(password)) {
- System.out.println("Password must have at least 2 digits");
- }
- //валидна -> дължината е валидна, съдържанието е правилно и бр. цифрите >= 2
- if (isValidLength(password) && isValidContent(password) && isValidCountDigits(password)) {
- System.out.println("Password is valid");
- }
- }
- public static boolean isValidLength (String password) {
- return password.length() >= 6 && password.length() <= 10;
- }
- public static boolean isValidContent (String password) {
- for (int position = 0; position <= password.length() - 1; position++) {
- char currentSymbol = password.charAt(position);
- if (!Character.isLetterOrDigit(currentSymbol)) {
- return false;
- }
- }
- return true;
- }
- public static boolean isValidCountDigits (String password) {
- int count = 0; //брой на цифри
- for (int position = 0; position <= password.length() - 1; position++) {
- char currentSymbol = password.charAt(position);
- if (Character.isDigit(currentSymbol)) {
- count++;
- }
- }
- //знам брой на цифрите
- return count >= 2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment