Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class PasswordValidator {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- String password = scan.nextLine();
- printPassHave2Digits(password);
- printPassLegitSymbols(password);
- printPassRange(password);
- boolean isTrue = printPassHave2Digits(password) && printPassLegitSymbols(password) && printPassRange(password);
- if (isTrue){
- System.out.println("Password is valid");
- }else{
- }
- }
- private static boolean printPassHave2Digits(String input) {
- int digitCount = 0;
- for (int i = 0; i < input.length(); i++) {
- char temp = input.charAt(i);
- if (temp >= '0' && temp <= '9') {
- digitCount++;
- }
- }
- if (digitCount < 2) {
- System.out.println("Password must have at least 2 digits");
- return false;
- }else {
- return true;
- }
- }
- private static boolean printPassLegitSymbols(String input) {
- for (int i = 0; i < input.length(); i++) {
- char symbol = input.charAt(i);
- if ((symbol >= 'a' && symbol <= 'z') || (symbol >= 'A' && symbol <= 'Z') || (symbol >= '0' && symbol <= '9')) {
- } else {
- System.out.println("Password must consist only of letters and digits");
- return false;
- }
- }
- return true;
- }
- private static boolean printPassRange(String input) {
- int length = input.length();
- if (length >= 6 && length <= 10) {
- return true;
- } else {
- System.out.println("Password must be between 6 and 10 characters");
- return false;
- }
- }
- }
Add Comment
Please, Sign In to add comment