Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ExerciseMethoods;
- import java.util.Scanner;
- public class O4PasswordValidator {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String password = scanner.nextLine();
- if (between6to10Characters(password)) {
- System.out.println("Password must be between 6 and 10 characters");
- }
- if (onlyLettersAndDigits(password)) {
- System.out.println("Password must consist only of letters and digits");
- }
- if (mustHave2Digits(password)) {
- System.out.println("Password must have at least 2 digits");
- }
- if (!onlyLettersAndDigits(password) && !between6to10Characters(password) && !mustHave2Digits(password)) {
- System.out.println("Password is valid");
- }
- }
- public static boolean between6to10Characters(String password) {
- boolean ok = false;
- int total = password.length();
- if (total < 6 || total > 10) {
- ok = true;
- }
- return ok;
- }
- public static boolean onlyLettersAndDigits(String password) {
- boolean ok = false;
- int invalid = 0;
- for (int i = 0; i < password.length(); i++) {
- int element = password.charAt(i);
- if (element < 48 || element > 57 && element < 65 || element > 90 && element < 97 || element > 122) {
- invalid += 1;
- }
- }
- if (invalid > 0) {
- ok = true;
- }
- return ok;
- }
- public static boolean mustHave2Digits(String password) {
- boolean ok = false;
- int digits = 0;
- for (int i = 0; i < password.length(); i++) {
- int element = password.charAt(i);
- if (element >= 48 && element <= 57) {
- digits += 1;
- }
- }
- if (digits < 2) {
- ok = true;
- }
- return ok;
- }
- }
Add Comment
Please, Sign In to add comment