Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class PaswwordValidator {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- String password = scan.nextLine();
- Boolean isCorectpass = getValidpass(password);
- if (isCorectpass) {
- System.out.println("Password is valid");
- }
- }
- public static Boolean getValidpass(String password) {
- boolean isCoerect1 = getPaswwordLenght(password);
- boolean isCoerect2 = getCheckLettersOrDigits(password);
- boolean isCoerect3 = getCheckAtLeastTwoDigit(password);
- if (isCoerect1 && isCoerect2 && isCoerect3) {
- return true;
- }
- return false;
- }
- // Проверка дали има минимум 2 числа
- public static boolean getCheckAtLeastTwoDigit(String password) {
- int counter = 0;
- for (int i = 0; i < password.length(); i++) {
- char symbol = password.charAt(i);
- int symbolInDigit = (int) symbol;
- if (symbolInDigit >= 48 && symbolInDigit <= 57) {
- counter++;
- }
- if (password.length() - 1 == i) {
- if (counter < 2) {
- System.out.println("Password must have at least 2 digits");
- return false;
- }
- }
- }
- return true;
- }
- //Проверка дали има само букви и цифри
- public static boolean getCheckLettersOrDigits(String password) {
- for (int i = 0; i < password.length(); i++) {
- char symbol = password.charAt(i);
- if (!Character.isLetterOrDigit(symbol)) {
- System.out.println("Password must consist only of letters and digits");
- return false;
- }
- }
- return true;
- }
- // Проверка дали дължината на паролата е от 6 до 10 симовола включително
- public static boolean getPaswwordLenght(String password) {
- if (password.length() >= 6 && password.length() <= 10) {
- return true;
- } else {
- System.out.println("Password must be between 6 and 10 characters");
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment