Write a Java program that prompts the user to enter a security code that matches a specific pattern. Your program must approve the user's entry.. Here is the pattern:
A lower case character, an upper case character, a lower case character, an upper case character, 3 or 4 digits, 1 or 2 lower case letters, 2 upper case characters, 1 digit.
public class SecurityCode { public static void main(String[] args) { System.out.println("Please, enter a security code"); Scanner in = new Scanner(System.in); String str = in.nextLine(); System.out.println("Is your code approved: " + isMatch(str));
}
/** * check your security code * @param str * @return true is it match the pattern */ public static boolean isMatch(String str) { Pattern pattern = Pattern.compile("[a-z][A-Z][0-9]{3,4}[a-z]{1,2}[A-Z]{2}[0-9]"); Matcher matcher = pattern.matcher(str); return matcher.matches(); } }
Comments
Leave a comment