Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class Pr4PasswordValidator {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String password = reader.readLine();
- if (checkNumbersOfCharaptes(password) && checkAllowedSymbols(password) && checkNumberOfDigits(password)) {
- System.out.println("Password is valid");
- return;
- }
- if (!(checkNumbersOfCharaptes(password))) {
- System.out.println("Password must be between 6 and 10 characters");
- if (!(checkNumberOfDigits(password))) {
- System.out.println("Password must have at least 2 digits");
- }
- return;
- }
- if (!(checkAllowedSymbols(password))) {
- System.out.println("Password must consist only of letters and digits");
- }
- if (!(checkNumberOfDigits(password))) {
- System.out.println("Password must have at least 2 digits");
- }
- }
- private static boolean checkNumbersOfCharaptes(String password) {
- boolean checkNumbersOfCharaptes = false;
- if (password.length() >= 6 && password.length() <= 10) {
- checkNumbersOfCharaptes = true;
- }
- return checkNumbersOfCharaptes;
- }
- private static boolean checkAllowedSymbols(String password) {
- boolean checkSymbols = false;
- int countLetters = 0;
- int coutDigits = 0;
- for (int i = 0; i < password.length(); i++) {
- //Digits
- int digit = password.charAt(i) - '0';
- if (digit >= 0 && digit <= 9) {
- coutDigits++;
- }
- //Letters
- if ((password.charAt(i) >= 65 && password.charAt(i) <= 90) ||
- (password.charAt(i) >= 97 && password.charAt(i) <= 122)) {
- countLetters++;
- }
- }
- //Result
- if (countLetters > 0 && coutDigits > 0) {
- checkSymbols = true;
- }
- return checkSymbols;
- }
- private static boolean checkNumberOfDigits(String password) {
- boolean checkNumberOfDigits = true;
- int coutDigits = 0;
- for (int i = 0; i < password.length(); i++) {
- if (password.charAt(i) >= 48 && password.charAt(i) <= 57) {
- coutDigits++;
- }
- }
- if (coutDigits <= 2) {
- checkNumberOfDigits = false;
- }
- return checkNumberOfDigits;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement