Java implementation LUHM algorithm--Verification of bank card number validity

Source: Internet
Author: User
The bank card is made up of "card issuing bank identification code + custom + check code" and other parts.
The most direct difference between a UnionPay standard card and a bank card issued in the past is the difference between the first 6 digits of the card number.
The first 6 digits of the bank card number are used to denote the issuing bank or institution, called the "issuing bank identification code" (Bank identification numbers, abbreviated as "BIN").

UnionPay standard card is made by domestic commercial banks (including postal storage, credit cooperatives) jointly issued, in line with the business norms and technical standards of UnionPay, card front right corner with "UnionPay" logo (Currently, the new issue of UnionPay standard card must have an international UnionPay new logo, the new non-UnionPay standard card using the old network Universal UnionPay logo), Card number of the top 6 to 622126 to 622925 of the bank card, is the Chinese bank card industry common national brand.

Package Com.it.demo;

Import Java.util.Scanner;
		public class Luhmtest {public static void main (string[] args) {Scanner input = new Scanner (system.in);
		System.out.println ("Please enter the bank card number:");
		String bankno = Input.next ();
		char res = getbankcardcheckcode (bankno.substring (0, Bankno.length ()-1));
			if (res!= ' N ') {System.out.println ("checksum result is:" +res);
			String CHARJX = bankno.substring (Bankno.length ()-1);//Deposit card number without check digit System.out.println ("Check digit of bank card is:" +CHARJX); if (Charjx.equals (string.valueof (res)) {System.out.println ("bank card is legal.")
			"); }else{System.out.println ("bank card is not legal.")
			"); 
     }}/** * The bank card number with no check bit is used to obtain the check digit with LUHM check algorithm: * 1, starting from the last digit of the card number, the inverse will add the odd digits (1, 3, 5, etc.). 
     * 2, starting from the last digit card number, reverse the number of even digits, multiplied by 2 (if the product is two digits, then subtract 9), and then sum.
	 * 3, the sum of odd digits plus the sum of even digits, the result should be divisible by 10. */public static char Getbankcardcheckcode (String noncheckcodecardid) {if Noncheckcodecardid = null | | nonchec Kcodecardid.trim (). Length () = 0 | | !noncheckcodecardid.matches ("\\d+") ||noncheckcodecardid.trim (). Length () <15 | | Noncheckcodecardid.trim (). Length () >18) {//If the transmitted data is not valid returns N System.out.println ("the bank card number is not valid.")
	           ");
	       Return ' N ';  
	       } char[] CHS = Noncheckcodecardid.trim (). ToCharArray (); 
	       int luhmsum = 0;  
	           Executes the LUH algorithm for (int i = chs.length-1, j = 0; I >= 0; I--, j +) {int k = chs[i]-' 0 ';  
	               if (j% 2 = 0) {//even digit processing K *= 2;  
	           K = k/10 + k% 10;             
	       } luhmsum = k; Return (luhmsum% 10 = 0)?  
	   ' 0 ': (char) ((10-luhmsum% 10) + ' 0 '); }  

}


Related Article

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.