In Java, Password Pattern Validation

import java.util.Scanner;

public class ValidatePassword{
	public static void main(String[]args){
		String pswd;
		int digi, uper, lowr;
		char[]chars = null;
		final int MIN = 2;
		final String ERROR = "%nError: At least %d %s is mandate!";
		Scanner user = new Scanner(System.in);
		do{
			digi = uper = lowr = 0;
			chars = null;
			System.out.print("\nEnter password: ");
			if(!user.hasNextLine()) break;
			pswd = user.nextLine();
			if(null == pswd) break;
			chars = pswd.toCharArray();
			for(char c: chars){
				if(Character.isUpperCase(c)){
					uper ++;
				} else if(Character.isLowerCase(c)){
					lowr ++;
				} else if(Character.isDigit(c)){
					digi ++;
				}
			}
			if(MIN > digi){
				System.out.printf(ERROR, MIN, "digits");
			} else if(MIN > uper){
				System.out.printf(ERROR, MIN, "upper case");
			} else if(MIN > lowr){
				System.out.printf(ERROR, MIN, "lower case");
			} else {
				break;
			}
		}while(true);
		if(null != chars){
			System.out.println("Success: your password is good.");
		} else {
			System.out.println("Error: your password is disapproved.");
		}
	}
}
Download

Comments

Popular posts from this blog