Luhn algorithm in Java
October 09, 2009 02:56:44 Last update: October 09, 2009 02:56:44
The Luhn algorithm is used to validate credit card numbers. This class provides two methods: one to validate that a number passes the Luhn algorithm validation, the other to generate a check digit that makes a number pass the Luhn validation.
public class Luhn { public static void main(String[] args) { if (args.length < 1) { System.err.println("Usage: number [g]"); System.exit(1); } String n = args[0]; if (args.length == 2) { System.out.println(n + generateDigit(n)); } else if (isValidNumber(n)) { System.out.println(n + " is valid"); } else { System.out.println(n + " is NOT valid"); } } private static boolean isValidNumber(String s) { return doLuhn(s, false) % 10 == 0; } private static String generateDigit(String s) { int digit = 10 - doLuhn(s, true) % 10; return "" + digit; } private static int doLuhn(String s, boolean evenPosition) { int sum = 0; for (int i = s.length() - 1; i >= 0; i--) { int n = Integer.parseInt(s.substring(i, i + 1)); if (evenPosition) { n *= 2; if (n > 9) { n = (n % 10) + 1; } } sum += n; evenPosition = !evenPosition; } return sum; } }
Easy email testing with http://www.ximailstop.com