Skip to content

Instantly share code, notes, and snippets.

@KristineNessa
Last active June 23, 2023 15:54
Show Gist options
  • Select an option

  • Save KristineNessa/5499031 to your computer and use it in GitHub Desktop.

Select an option

Save KristineNessa/5499031 to your computer and use it in GitHub Desktop.
The Java code below is an example of how we can validate the "check digit" of a UPC (Universal Product Code) 12 digit code. There are multiple ways the Luhn algorithm can be implemented using code. This Gist is designed to get the thought process rolling and to see how others would modify the code to be more efficient for the same 12 digit UPC "…
/**
* Validate a UPC code by calculating for the check digit number.
*/
import java.util.Scanner;
public class ValidateUPCCode {
// Main function
public static void main(String[] args) {
final long LARGEST_POSSIBLE_UPC_CODE = 999999999999L;
// Read UPC Code
Scanner inputUPCCodeStr = new Scanner(System.in);
System.out.print("Enter a 12-digit UPC code: ");
long inputUPCCode = inputUPCCodeStr.nextLong();
long codeValue = inputUPCCode;
// Check UPC code size and validate UPC code against the check digit number.
if ((codeValue > 0) && (codeValue < LARGEST_POSSIBLE_UPC_CODE)) {
// Get the individual digits of the UPC code, in reverse order
int n12 = (int) (codeValue % 10);
codeValue /= 10;
int n11 = (int) (codeValue % 10);
codeValue /= 10;
int n10 = (int) (codeValue % 10);
codeValue /= 10;
int n9 = (int) (codeValue % 10);
codeValue /= 10;
int n8 = (int) (codeValue % 10);
codeValue /= 10;
int n7 = (int) (codeValue % 10);
codeValue /= 10;
int n6 = (int) (codeValue % 10);
codeValue /= 10;
int n5 = (int) (codeValue % 10);
codeValue /= 10;
int n4 = (int) (codeValue % 10);
codeValue /= 10;
int n3 = (int) (codeValue % 10);
codeValue /= 10;
int n2 = (int) (codeValue % 10);
codeValue /= 10;
int n1 = (int) (codeValue % 10);
codeValue /= 10;
// Get sum of the first 5 odd digit numbers
int oddSum = n1 + n3 + n5 + n7 + n9 + n11;
// Get sum of first 5 even digit numbers
int evenSum = n2 + n4 + n6 + n8 + n10;
// Multiply the oddSum value by 3 and add to the evenSum value. Subtract the remainder
// of result/10 (result modulo 10) from 10 to get the calculated check digit
int calcCheckDigit = 10 - ((evenSum + 3 * oddSum) % 10);
if (calcCheckDigit == n12) {
System.out.println(inputUPCCode + " is a valid UPC code");
} else {
System.out.println(inputUPCCode + " is not a valid UPC code");
}
} else {
System.out.println(inputUPCCode + "is not a valid UPC code");
}
}
}
@a-schild
Copy link
Copy Markdown

This code fails for all UPC codes with a check digit of 0.
For example 190198160980 is not accepted, since the calcCheckDigit results in a value of 10 (instead of 0)

Changing line 56 solves that problem:
int calcCheckDigit = (10 - ((evenSum + 3 * oddSum) % 10)) % 10;

@racca3141
Copy link
Copy Markdown

https://github.com/racca3141/UPC
Thanks @a-schild. I made the same mistake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment