Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MethodsExcercise;
- import java.util.Scanner;
- public class P04PasswordValidator {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String password = scanner.nextLine();
- System.out.println(printIfIsValid(password));
- printError(printIfNeedMoreOrLessChars(password));
- printError(printIfNotLettersDigitsOnly(password));
- printError(printIfNeedDigits(password));
- }
- static void printError(String error){
- if (error != "")
- System.out.println(error);
- }
- static String printIfNeedMoreOrLessChars (String password) {
- int length = password.length();
- String letter = "";
- if ((length < 6) || (length > 10)) {
- letter = "Password must be between 6 and 10 characters";
- }
- return letter;
- }
- static String printIfNotLettersDigitsOnly (String password) {
- int length = password.length();
- boolean onlyLettersDigits = true;
- String letter2 = "";
- for (int i = 0; i < length; i++) {
- char currentChar = password.charAt(i);
- if (currentChar < 48) {
- onlyLettersDigits = false;
- } else if (currentChar > 57 && currentChar < 65) {
- onlyLettersDigits = false;
- } else if (currentChar > 90 && currentChar < 97) {
- onlyLettersDigits = false;
- } else if (currentChar > 122) {
- onlyLettersDigits = false;
- }
- if (!onlyLettersDigits) {
- letter2 = "Password must consist only of letters and digits";
- }
- }
- return letter2;
- }
- static String printIfNeedDigits (String password) {
- int length = password.length();
- int digits = 0;
- String letter3 = "";
- for (int i = 0; i < length; i++) {
- char currentChar = password.charAt(i);
- if (currentChar >= 48 && currentChar <= 57) {
- digits++;
- }
- }
- if (digits < 2) {
- letter3 = "Password must have at least 2 digits";
- }
- return letter3;
- }
- static String printIfIsValid (String password) {
- int length = password.length();
- int digits1 = 0;
- String letter4 = "";
- int onlyLettersDigits1 = 0;
- for (int i = 0; i < length; i++) {
- char currentChar = password.charAt(i);
- if (currentChar < 48) {
- onlyLettersDigits1++;
- } else if (currentChar > 57 && currentChar < 65) {
- onlyLettersDigits1++;
- } else if (currentChar > 90 && currentChar < 97) {
- onlyLettersDigits1++;
- } else if (currentChar > 122) {
- onlyLettersDigits1++;
- }
- if (currentChar >= 48 && currentChar <= 57) {
- digits1++;
- }
- }
- if (digits1 >= 2) {
- if (length >= 6 && length <= 10) {
- if (onlyLettersDigits1 == 0) {
- letter4 = "Password is valid";
- }
- }
- }
- return letter4;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement