Write a java program to validate PAN no.

java program to validate PAN no. Write a program to read a string and validate PAN no. against following rules:

  1. There must be eight characters.
  2. First three letters must be alphabets followed by four digit number and ends with alphabet
  3. All alphabets should be in capital case.

Print “Valid” if the PAN no. is valid, else print “Invalid”.

The return type (Integer) should return 1 if the string is a valid PAN no. else return 2.

java program to validate PAN no

package com.demo;
 
import java.util.*;
 
public class Main {
 
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1= sc.next();
getvalues(s1);
}
 
public static void getvalues(String s1) {
if(s1.matches("[A-Z]{3}[0-9]{4}[A-Z]{1}"))
{
System.out.println("Valid");
}
else
System.out.println("Invalid");
}
}

Input and Output Format:

Input consists of a string, which corresponds to the PAN number.

Output consists of a string – “Valid” or “Invalid”

Refer sample output for formatting specifications.

Sample Input 1:

ALD3245E

Sample Output 1:

Valid

Sample Input 2:

OLE124F

Sample Output 2:

Invalid

Add a Comment

Your email address will not be published. Required fields are marked *

3 × 2 =