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