Bank card number LUHM check Java implementation code

Source: Internet
Author: User

1 ImportJava.util.regex.Matcher;2 ImportJava.util.regex.Pattern;3 4  Public classCheckbanknumber {5 6 7     //Description: Bank card number Luhm check8 9     //luhm Check rule: 16-digit bank card number (19-bit generic):Ten      One     //1. Place the 15 (or 18) position card number without the check digit from right to 1 to 15 (18) and multiply the number on the odd digit by 2.  A     //2. Add all 10 bits of the singular product, plus the numbers on all even digits.  -     //3. The addition and calibration potentials are divisible by 10.  -      the      -     //beginning 6 bits -     //private final static String Strbin = " 10,18,30,35,37,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,58,60,62,65,68,69,84,87,88,94,95,98,99 "; -  +     Private Final StaticString success= "true"; -      +     Private Final StaticString bad_length= "bank card number must be between 16 and 19"; A      at     Private Final StaticString not_number= "bank cards must all be numbers"; -      -     Private Final StaticString illegal_number= "bank card not in conformity with rules"; -      -      Public Staticstring Luhmcheck (String bankno) { -         if(Bankno.length () < | | Bankno.length () > 19) { in             returnbad_length; -             } to          +Pattern pattern = Pattern.compile ("[0-9]*"); -Matcher match =Pattern.matcher (bankno); the         if(match.matches () = =false) { *             returnNot_number; $         }Panax Notoginseng          - //if (strbin.indexof (bankno.substring (0, 2) = = = 1) { the //return "bank card number at the beginning of the 6-bit non-conforming specification"; + //        } A          the         intLastnum=integer.parseint (Bankno.substring (Bankno.length () -1,bankno.length ());//take out the last one (compare with Luhm) +          -String first15num=bankno.substring (0,bankno.length ()-1);//Top 15 or 18 digits $         //System.out.println (first15num); $         Char[] NewArr =New Char[First15num.length ()];//Flashback loading Newarr -         Char[] Temparr =First15num.tochararray (); -          for(inti = 0; i < temparr.length; i++){ theNewarr[temparr.length-1-i] =Temparr[i]; -         }Wuyi         //System.out.println (NEWARR); the          -          Wu         int[] Arrsinglenum =New int[Newarr.length];//the product <9 of odd digit -         int[] arrSingleNum2 =New int[Newarr.length];//the product >9 of odd digit About         int[] arrdoublenum=New int[Newarr.length];//array of even digits $          -          for(intj = 0;j < Newarr.length; J + +){ -             if((j+1)%2==1) {//Odd digits -                 if((int) (newarr[j]-48) *2<9) AArrsinglenum[j]= (int) (newarr[j]-48) * *; +                 Else theArrsinglenum2[j]= (int) (newarr[j]-48) * *; -             } $             Else //even digits theArrdoublenum[j]= (int) (newarr[j]-48); the         } the          the         int[] Arrsinglenumchild =New int[Newarr.length];//Odd Digit * * >9 after the split of the array single digit -         int[] Arrsinglenum2child =New int[Newarr.length];//Odd Digit * * >9 array 10-bit number after the split in          the          for(inth=0;h<arrsinglenum2.length;h++){ theARRSINGLENUMCHILD[H] = (Arrsinglenum2[h])%10; AboutARRSINGLENUM2CHILD[H] = (arrsinglenum2[h])/10; the         }     the          the         intsumsinglenum=0;//the sum of an array of odd digits, < 9 +         intsumdoublenum=0;//The sum of an even-bit array -         intSumsinglenumchild=0;//The sum of the single digits of the array after the >9 of odd digits the         intSumsinglenum2child=0;//The sum of 10 digits of the array after the >9 of odd digitsBayi         intSumtotal=0; the          for(intm=0;m<arrsinglenum.length;m++){ thesumsinglenum=sumsinglenum+Arrsinglenum[m]; -         } -          the          for(intn=0;n<arrdoublenum.length;n++){ thesumdoublenum=sumdoublenum+Arrdoublenum[n]; the         } the          -          for(intp=0;p<arrsinglenumchild.length;p++){ theSumsinglenumchild=sumsinglenumchild+Arrsinglenumchild[p]; theSumsinglenum2child=sumsinglenum2child+Arrsinglenum2child[p]; the         }      94          theSumtotal=sumsinglenum+sumdoublenum+sumsinglenumchild+Sumsinglenum2child; the          the         //Calculate Luhm Value98         intk= sumtotal%10==0?10:sumtotal%10;  About         intLuhm= 10-K; -         101         if(Lastnum = =Luhm) {102             returnSUCCESS;//Validation by103         }104         Else{ the             returnIllegal_number;106         }      107     }108     109      Public Static voidMain (string[] args) { theString bankno = "6214857110432079";111System.out.println (bankno+ ":" +Luhmcheck (Bankno)); theString bankno1 = "6212164000046323291";113System.out.println (bankno1+ ":" +Luhmcheck (Bankno1)); theString Bankno2 = "6222023104004123546"; theSystem.out.println (bankno2+ ":" +Luhmcheck (Bankno2)); the     }117}

Bank card number LUHM check Java implementation code

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.