Java: 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 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:
Flowchart :
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 :
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-exercises/re/java-re-exercise-18.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics