Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MethodsExercises;
- import java.util.Scanner;
- public class PasswordValidator04 {
- static boolean countLetters(String password){
- if (!(password.length()>=6 && password.length()<=10)){
- System.out.println("Password must be between 6 and 10 characters");
- return false;
- }
- return true;
- }
- static boolean charContains(String password){
- for (char i=0; i<password.length();i++) {
- char symbols=password.charAt(i);
- if (!Character.isLetterOrDigit(symbols)){
- System.out.println("Password must consist only of letters and digits");
- return false;
- }
- }
- return true;
- }
- static boolean twoDigits(String password){
- int count=0;
- for (int i = 0; i < password.length() ; i++) {
- char symbols=password.charAt(i);
- if (Character.isDigit(symbols)){
- count++;
- }
- }
- if (count>=2){
- return true;
- }else {
- System.out.println("Password must have at least 2 digits");
- return false;
- }
- }
- static void passwordIsValid(String password){
- boolean check1=countLetters(password);
- boolean check2=charContains(password);
- boolean check3=twoDigits(password);
- if (check1&&check2&&check3){
- System.out.println("Password is valid");
- }
- }
- public static void main(String[] args) {
- Scanner scanner=new Scanner(System.in);
- String password=scanner.nextLine();
- passwordIsValid(password);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement