Answer to Question #284226 in Java | JSP | JSF for jad

Question #284226

Write a Java program to check the validity of a given employee ID using RegEx.

An employee ID is valid if and only if consists of 8 to 10 characters in which the first two characters is 01 or 02 or 03 followed by 4-6 alphabetic characters followed by 2 numeric digits.


1
Expert's answer
2022-01-02T18:17:24-0500


import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class App {


	/**
	 * The start point of the program
	 * 
	 * @param args
	 * 
	 */
	public static void main(String[] args) {
		Scanner keyBoard = new Scanner(System.in);


		System.out.print("Enter employee ID: ");
		String employeeID = keyBoard.nextLine();


		if (isEmployeeIDValid(employeeID)) {
			System.out.println("Employee ID is valid");
		} else {
			System.out.println("Employee ID is NOT valid");
		}


		keyBoard.close();
	}


	/**
	 * An employee ID is valid if and only if consists of 8 to 10 characters in
	 * which the first two characters is 01 or 02 or 03 followed by 4-6 alphabetic
	 * characters followed by 2 numeric digits.
	 * 
	 * @param employeeID
	 * @return
	 */
	private static boolean isEmployeeIDValid(String employeeID) {
		if (employeeID.length() < 8 || employeeID.length() > 10) {
			return false;
		}
		final String ID_REGEX = "^0" + "[0-3]" + "[a-zA-Z]{4,6}" + "[0-9]" + "[0-9]";
		Pattern pattern = Pattern.compile(ID_REGEX);
		Matcher matcher = pattern.matcher(employeeID);
		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 the first!

Leave a comment

LATEST TUTORIALS
New on Blog