Verifying that the ID card is a legitimate algorithm

Source: Internet
Author: User

You can enter the first 17 digits of the ID card, and then figure out 18 bits, with the input of the 18th bit to verify that the input is a valid ID card "

Package Com.edaixi.testjava;import Java.math.mathcontext;import Java.util.regex.matcher;import Java.util.regex.pattern;public class Testjavatool {private final static int[] FACTOR = new int[]{        7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2}; /** * Calculates the sum of the first 17-digit ID number multiplied by the coefficient * @param id17 first 17-digit ID number * @return */private static int sumfactor (String id1 7) {if (Id17==null | | Id17.trim (). Equals ("")) {return-1;        The ID entered is empty} int len = Id17.length ();        if (len!=17) {return-3;//Enter the ID number not to be 17-bit} int sum = 0;        for (int i=0; i<len; i++) {sum + = factor[i] * Integer.parseint (string.valueof (Id17.charat (i)));    } return sum; /** * Based on the first 17 digits of the ID number, figure out the 18th digit * @param id17 * @return * * * public static String Getlastnumofid (Str        ing id17) {int sum = Sumfactor (ID17);        String res = "";        if (sum==-1) {return "entered ID is empty";       }else if (sum==-3) {     Return "The entered ID number is not 17-bit";            }else {int mod = sum% 11;                Switch (MoD) {case 0:res = "1";            Break                Case 1:res = "0";            Break                Case 2:res = "X";            Break                Case 3:res = "9";            Break                Case 4:res = "8";            Break                Case 5:res = "7";            Break                Case 6:res = "6";            Break                Case 7:res = "5";            Break                Case 8:res = "4";            Break                Case 9:res = "3";            Break                Case 10:res = "2";            Break            Default:break;    }} return res; }/** * Determine if the ID entered is legal * @param idnum 18-digit ID number * @return */public static Boolean Iscorrectid (String idnum) {Boolean flag = false;        if (Idnum==null | | Idnum.trim (). Length ()!=18) {return flag;        } String last = Getlastnumofid (idnum.substring (0, Idnum.length ()-1));        if (Last.equals (string.valueof (Idnum.charat (Idnum.length ()-1))) {flag = true;    } return flag; } public static void Main (string[] args) {System.out.println ("The Last of the IDs is:" +getlastnumofid ("41270219910105361        "));    SYSTEM.OUT.PRINTLN ("ID card is lawful:" +iscorrectid ("41270219910105361")); }}

  

Verifying that the ID card is a legitimate algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.