w3resource

Java programming Exercises: Validate a personal identification number (PIN)

Java Regular Expression: Exercise-18 with Solution

(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 the length of 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).

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Count the number of decimal places in a given number.

Next: Remove the specific letters from a string and return the new string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Share this Tutorial / Exercise on : Facebook and Twitter

Java: Tips of the Day

What is the best way to filter a Java Collection?

Java 8 (2014) solves this problem using streams and lambdas in one line of code:

List<Person> beerDrinkers = persons.stream()
    .filter(p -> p.getAge() > 16).collect(Collectors.toList());

Use Collection#removeIf to modify the collection in place. (Notice: In this case, the predicate will remove objects who satisfy the predicate):

persons.removeIf(p -> p.getAge() <= 16);

lambdaj allows filtering collections without writing loops or inner classes:

List beerDrinkers = select(persons, having(on(Person.class).getAge(),
    greaterThan(16)));

Ref: https://bit.ly/3uwYid6