Read the PAN Number and Name of an Employee. If the PAN Number does not contain exactly 10 characters and if the Name of the Employee is empty throw an IllegalArgumentException. If the PAN Number contains any character other than digits and alphabets(Vice versa), throw a NoSuchElementException. Considering the PAN number starts with 0 to 9 position, check for 0th to 4th (including 4th position) to be alphabets, check for 5th to 8th position (including 5th &8th position) of the PAN Number to be digit, again 9th positon must be alphabet and also 4th position of the PAN Number should be the First Letter of the employee's Name. If all the cases are valid for PAN Number print the message ‘valid’ else ‘invalid’.
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Q173951 {
/**
* The start point of the program
*
* @param args
*/
public static void main(String[] args) {
// Create Scanner object
Scanner keyboard = new Scanner(System.in);
try {
// Get input from the user
System.out.print("Enter the PAN number of the employee: ");
String PANNumber = keyboard.nextLine();
System.out.print("Enter the name of the employee: ");
String name = keyboard.nextLine();
// If the PAN Number does not contain exactly 10 characters and
// if the Name of the Employee is empty throw an IllegalArgumentException.
if (PANNumber.length() != 10 || name.isEmpty()) {
throw new IllegalArgumentException("The PAN Number does not contain exactly 10 characters or the name of the Employee is empty!");
}
// If the PAN Number contains any character other than digits and alphabets(Vice
// versa), throw a NoSuchElementException.
if(!isLetterAndDigit(PANNumber)) {
throw new NoSuchElementException("The PAN Number contains any character other than digits and alphabet!");
}
// If all the cases are valid for PAN Number print the message "valid" else "invalid".
if (checkPANNumber(PANNumber, name)) {
System.out.println("PAN Number is valid.");
}else {
System.out.println("PAN Number is invalid.");
}
} catch (Exception e) {
// Display error message
System.out.println(e.getMessage());
}
// close Scanner
keyboard.close();
}
/***
* Check if PANNumber has only the letter and the digit
* @param PANNumber
* @return
*/
private static boolean isLetterAndDigit(String PANNumber) {
for (int i = 0; i < PANNumber.length(); i++) {
if (!(Character.isLetterOrDigit(PANNumber.charAt(i)))) {
return false;
}
}
return true;
}
/**
* Checking the PAN number starts with 0 to 9 position, check for 0th to 4th (including 4th position) to be alphabets,
* check for 5th to 8th position (including 5th &8th position) of the PAN Number to be digit, again 9th positon must be alphabet
* and also 4th position of the PAN Number should be the First Letter of the employee's Name.
* @param PANNumber
* @param name
* @return
*/
private static boolean checkPANNumber(String PANNumber,String name){
if (!(Character.isDigit(PANNumber.charAt(0)))) {
return false;
}
for (int i = 1; i <= 4; i++) {
if (!(Character.isLetter(PANNumber.charAt(i)))) {
return false;
}
}
if (PANNumber.charAt(4)!=name.charAt(0)) {
return false;
}
for (int i =5 ; i <= 8; i++) {
if (!(Character.isDigit(PANNumber.charAt(i)))) {
return false;
}
}
if (!(Character.isLetter(PANNumber.charAt(9)))) {
return false;
}
return true;
}
}
Comments
Leave a comment