68 310
Assignments Done
99%
Successfully Done
In November 2018

Answer to Question #62193 in Java | JSP | JSF for Bill DeShields

Question #62193
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.
Expert's answer
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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();
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions