Created
October 30, 2015 18:20
Revisions
-
DavisDevelopment created this gist
Oct 30, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ public class Luhn { /** * validate the given card-number with the luhn mod10 algorithm */ public static boolean luhn(String card) { int total = 0; // sum of all digits int v; // variable to hold the numeric value of the "current" char int i = card.length(); // current position in the String /* [action] represents what we're currently doing with the chars we read ------ action=true: odd character action=false: even character */ boolean action = false; while(0 < i--) { // get the numeric value of the char v = (int)(card.charAt(i) - 48); // if we're an even number of steps into this loop if ( action ) { v *= 2; // double the value of [v] // and if [v] is now a double-digit number if (v > 9) { // reduce it by 9, effectively adding the two digits v -= 9; } } // increase [total] by total += v; // toggle the value of [action] action = !action; } /* return whether [total] is divisible by 10 */ return ((total % 10) == 0); } }