Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Fundamentals.Methods.Exercises;
- import java.util.Scanner;
- public class PasswordValidator {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- checker(input);
- }
- public static void checker(String text){
- String[] invalidSymbols = {"+","-","'","§","$","%","&","/","(",",",")","=","?","!","`",":",";","*","~"};
- boolean passwordLength = false;
- if (text.length() < 6 || text.length() > 10){
- passwordLength = true;
- }
- boolean passwordDigits = false;
- for (int i = 0; i < text.length(); i++) {
- for (int j = 0; j < invalidSymbols.length; j++) {
- String currentSymbol = invalidSymbols[j];
- char badSymbol = currentSymbol.charAt(0);
- if (text.charAt(i) == badSymbol){
- passwordDigits = true;
- break;
- }
- if (passwordDigits){
- break;
- }
- }
- }
- boolean passwordNumbers = true;
- int count = 0;
- char[] number = {'1','2','3','4','5','6','7','8','9','0'};
- for (int i = 0; i < text.length(); i++) {
- for (int j = 0; j < number.length; j++) {
- char currentChar = text.charAt(i);
- char charArr = number[j];
- if (currentChar == charArr){
- count++;
- if (count == 2) {
- passwordNumbers = false;
- break;
- }
- }
- }
- if (count == 2){
- break;
- }
- }
- if (passwordLength){
- System.out.println("Password must be between 6 and 10 characters");
- }
- if (passwordDigits){
- System.out.println("Password must consist only of letters and digits");
- }
- if (passwordNumbers){
- System.out.println("Password must have at least 2 digits");
- }
- if (!passwordLength && !passwordDigits && !passwordNumbers){
- System.out.println("Password is valid");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment