Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package TechModule;
- import java.util.Scanner;
- public class p03_MethodsPasswordValidator {
- public static boolean isSymbol(String password, int a){
- boolean isSymbol=false;
- for (int i = 0; i < a; i++) {
- if ((password.charAt(i) >= 65 && password.charAt(i) <= 90)
- ||(password.charAt(i) >= 97 && password.charAt(i) <= 122)
- ||(password.charAt(i) >= 48 && password.charAt(i) <= 57)){
- isSymbol = true;
- } else {
- isSymbol=false;
- break;}
- }
- return isSymbol;}
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String password = scanner.nextLine();
- int count = 0;
- boolean isDigit = false;
- boolean passwordCharacters = true;
- boolean symbol=isSymbol(password, password.length());
- if (password.length() > 10 || password.length() < 6) {
- System.out.println("Password must be between 6 and 10 characters");
- passwordCharacters = false;
- }
- for (int i = 0; i < password.length(); i++) {
- if (password.charAt(i) >= 48 && password.charAt(i) <= 57) {
- count++;
- isDigit = true;
- }
- }
- if (symbol == false) {
- System.out.println("Password must consist only of letters and digits");
- }
- if (count <= 2) {
- System.out.println("Password must have at least 2 digits");
- }
- if (passwordCharacters && isDigit && symbol==true) {
- System.out.println("Password is valid");
- }
- }
- }
- --------------------------------------------------------------------------------------------------------------------------
- сетих се ,че мога да превърна всички букви в малки примерно и да премахна проверката за големи букви... обаче, още някъде бъркам ... ???
- ----------------------------------------------------------------------------------------
- Write a program that checks if a given password is valid. Password rules are:
- • 6 – 10 characters (inclusive)
- • Consists only of letters and digits
- • Have at least 2 digits
- If a password is valid print “Password is valid”. If it is not valid, for every unfulfilled rule print a message:
- • "Password must be between 6 and 10 characters"
- • "Password must consist only of letters and digits"
- • "Password must have at least 2 digits"
- Examples
- Input Output
- logIn Password must be between 6 and 10 characters
- Password must have at least 2 digits
- MyPass123 Password is valid
- Pa$s$s Password must consist only of letters and digits
- Password must have at least 2 digits
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement