java

PHOTO EMBED

Thu Apr 22 2021 08:09:33 GMT+0000 (Coordinated Universal Time)

Saved by @ahmedqgqgq #java

import java.util.Scanner;

//Some websites impose certain rules for passwords,
//Write a java program that checks whether a string is valid passwords,
//suppose the password rule is as follows,
//A password must have at least eight characters,
//A password consists of only letters and digits, 
//A password must contain at least two digits,
//your program prompts the user to enter a password and displays"valid password",
//if the rule is followed or"invalid password otherwise",

public class Ok {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the password:");
        String password = input.next();
        System.out.println(password.length());
        if (isLeastEightChara(password) && isContainLeastTwoDigits(password) && isOnlyLettersandDigits(password)) {
            System.out.println("Valid password");
        } else {
            System.out.println("inValid password");
        }
        System.out.println(isLeastEightChara(password));
        System.out.println(isContainLeastTwoDigits(password));
        System.out.println(isOnlyLettersandDigits(password));

    }

    public static boolean isLeastEightChara(String password) {
        if (password.length() >= 8) {
            return true;
        } else {
            return false;
        }
    };

    public static boolean isContainLeastTwoDigits(String password) {
        int count = 0;
        for (int i = 0; i < password.length(); i++) {
            char pass = password.charAt(i);
            if (Character.isDigit(pass)) {
                count++;
            }
        }
        if (count >= 2) {
            return true;
        } else {
            return false;
        }
    };

    public static boolean isOnlyLettersandDigits(String password) {
        boolean ahmed = false;
        for (int i = 0; i < password.length(); i++) {
            char pass = password.charAt(i);
            if (Character.isDigit(pass)) {
                ahmed = true;
            } else if (Character.isAlphabetic(pass)) {
                ahmed = true;
            } else {
                ahmed = false;
            }
        }
        return ahmed;
    };

}
content_copyCOPY