w3resource

Java: Validate a personal identification number (PIN)


18. Validate PIN Code

(PIN): A personal identification number, or PIN, is a string of at least four digits used to unlock a bank account or card to which it has been assigned.

Write a Java program to validate a personal identification number (PIN). Assume a PIN number is 4, 6 or 8.

Sample Solution-1:

Java Code:

public class test {
 
   public static void main(String[] args) {	   
	    String n = "123";
		System.out.println("Original PIN Number: "+n);
		System.out.println("Is the said PIN number is valid? "+validate(n));
	    n = "1234";
		System.out.println("Original PIN Number: "+n);
		System.out.println("Is the said PIN number is valid? "+validate(n));
	    n = "12345";
		System.out.println("Original PIN Number: "+n);
		System.out.println("Is the said PIN number is valid? "+validate(n));	
	    n = "123456";
		System.out.println("Original PIN Number: "+n);
		System.out.println("Is the said PIN number is valid? "+validate(n));
	    n = "1234567";
		System.out.println("Original PIN Number: "+n);
		System.out.println("Is the said PIN number is valid? "+validate(n));	    
		n = "12345678";
		System.out.println("Original PIN Number: "+n);
		System.out.println("Is the said PIN number is valid? "+validate(n));
		n = "123456789";
		System.out.println("Original PIN Number: "+n);
		System.out.println("Is the said PIN number is valid? "+validate(n));		
        }

   public static boolean validate(String n) {
	   return n.matches("\\d{4}|\\d{6}|\\d{8}");
   }
}

Sample Output:

Original PIN Number: 123
Is the said PIN number is valid? false
Original PIN Number: 1234
Is the said PIN number is valid? true
Original PIN Number: 12345
Is the said PIN number is valid? false
Original PIN Number: 123456
Is the said PIN number is valid? true
Original PIN Number: 1234567
Is the said PIN number is valid? false
Original PIN Number: 12345678
Is the said PIN number is valid? true
Original PIN Number: 123456789
Is the said PIN number is valid? false

Pictorial Presentation:

Java Regular Expression: Validate a personal identification number(PIN).

Flowchart :

Flowchart: Validate a personal identification number (PIN).

Sample Solution-2:

Java Code:

public class test {
 
   public static void main(String[] args) {	   
	    String n = "123";
		System.out.println("Original PIN Number: "+n);
		System.out.println("Is the said PIN number is valid? "+validate(n));
	    n = "1234";
		System.out.println("Original PIN Number: "+n);
		System.out.println("Is the said PIN number is valid? "+validate(n));
	    n = "12345";
		System.out.println("Original PIN Number: "+n);
		System.out.println("Is the said PIN number is valid? "+validate(n));	
	    n = "123456";
		System.out.println("Original PIN Number: "+n);
		System.out.println("Is the said PIN number is valid? "+validate(n));
	    n = "1234567";
		System.out.println("Original PIN Number: "+n);
		System.out.println("Is the said PIN number is valid? "+validate(n));	    
		n = "12345678";
		System.out.println("Original PIN Number: "+n);
		System.out.println("Is the said PIN number is valid? "+validate(n));
		n = "123456789";
		System.out.println("Original PIN Number: "+n);
		System.out.println("Is the said PIN number is valid? "+validate(n));		
        }

   public static boolean validate(String n) {
	   return n.matches("[0-9]+") && n.length() == 4 || n.length() == 6 || n.length() == 8;
   }
}

Sample Output:

Original PIN Number: 123
Is the said PIN number is valid? false
Original PIN Number: 1234
Is the said PIN number is valid? true
Original PIN Number: 12345
Is the said PIN number is valid? false
Original PIN Number: 123456
Is the said PIN number is valid? true
Original PIN Number: 1234567
Is the said PIN number is valid? false
Original PIN Number: 12345678
Is the said PIN number is valid? true
Original PIN Number: 123456789
Is the said PIN number is valid? false

Flowchart :

Flowchart: Validate a personal identification number (PIN).


For more Practice: Solve these Related Problems:

  • Write a Java program to validate that a PIN is exactly 4, 6, or 8 digits long using a regex pattern.
  • Write a Java program to check if a given PIN contains only numeric characters and matches one of the allowed lengths.
  • Write a Java program to implement a method that returns true if the PIN meets the specified length criteria, false otherwise.
  • Write a Java program to design a PIN validation function that simultaneously checks for digit-only content and valid length.

Go to:


PREV : Count Decimal Places.

Next: Remove 'p', 'q', 'r' Letters.

Java Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.