
java program to accept the employee code from user and validate it according to the following logic. The first character of the employee code should be the letter 'E'. If it is any other letter, throw a user defined exception that displays an appropriate error message.

Here is an example that validates employee code. The given code accepts the code from the user and validate using regular expression. If the user starts the code with the capital letter E followed by other alphabets and numbers, then it will take otherwise it will show error message and allow the user to enter again.
import java.util.*;
class ValidateEmployee
{
public static boolean validateString(String st) {
return st.matches("[E]+[a-zA-Z0-9]*");
}
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.print("Enter code: ");
String code=input.next();
while (validateString(code) != true) {
System.out.print("Invalid String!");
System.out.println();
System.out.print("Enter String: ");
code = input.nextLine();
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.