Write a java program to validate PAN number and Mobile Number.
If it is invalid then throw user defined Exception “Invalid Data”, otherwise display
it.
class InvalidDataException extends Exception {
public
InvalidDataException(String message) {
super(message);
}
}
public class DataValidator {
public static void
main(String[] args) {
try {
String panNumber = "ABC12"; //
Replace with the PAN number to validate
String
mobileNumber = "1234567890"; // Replace with the mobile number to
validate
validatePAN(panNumber);
validateMobileNumber(mobileNumber);
System.out.println("Valid PAN Number: " + panNumber);
System.out.println("Valid Mobile Number: " + mobileNumber);
} catch
(InvalidDataException e) {
System.out.println("Error: " + e.getMessage());
}
}
public static void validatePAN(String
pan) throws InvalidDataException {
if
(pan.matches("[A-Z]{5}[0-9]{4}[A-Z]{1}")) {
// PAN is
valid
} else {
throw new
InvalidDataException("Invalid PAN Number");
}
}
public static void
validateMobileNumber(String mobileNumber) throws InvalidDataException {
if
(mobileNumber.matches("[0-9]{10}")) {
// Mobile
number is valid
} else {
throw new
InvalidDataException("Invalid Mobile Number");
}
}
}
C:\Program Files\Java\jdk-11.0.17\bin>javac
DataValidator.java
C:\Program Files\Java\jdk-11.0.17\bin>java DataValidator
Error: Invalid PAN Number
C:\Program Files\Java\jdk-11.0.17\bin>