Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Checksum digit is an important error detection measure for integrity check for m

ID: 3598339 • Letter: C

Question

Checksum digit is an important error detection measure for integrity check for many codes,
including UPC (Universal Product Code) barcodes. Typically, an UPC is a 12-digit number
where the last digit on the right serves as the checksum digit. The checksum digit can be
calculated based on other 11 digits of the UPC and if it does not match with the calculated
value, it is certain that there is some error in the code transmission.
The checksum digit in a 12-digit UPC is calculated as follows:
Step 1: Sum of all digits at odd positions (counting from 1, left to right, 1, 3, 5, 7, 9, 11)
Step 2: Sum of all digits at even positions except the last position (counting from left to
right, 2, 4, 8, 6, 10 – the 12th position is not included in the sum)
Step 3: Calculate (3*oddsum) + evensum
Step 4: If the result obtained at step 3 leaves remainder 0 when divided by 10, then
checksum is 0. Otherwise, the checksum is (10 – the remainder when divided by 10).
Programming:
A sample file has been provided with the assignment description. The file contains 10
different UPCs. Write a Java program to check whether the UPC codes in the sample files
are valid. If found invalid, indicate what would be the correct checksum digit (assuming
there is no error in other 11 digits).
Program execution requirements:
1. The program must accept the input file as a command line argument. The test input file
will have similar format as the sample file provided.
2. The output format should be indication of “valid” or “invalid” for each UPC on file. If
“invalid”, the output should also indicate the correct checksum digit value. You do not need
to write the output onto a file. Just display it on console.

Explanation / Answer

Given below is the code for validating checksum of UPC.

Note:

Since the sample input file is not provided in the question, I have assumed the file to contain only UPC codes. There will be no other data, even on first line. (not even no. of records). If the format is dfiferent from this , let me know through comments.

You will have to provide the filename as command line argument to the program. Make sure to have your file in project folder directly and not in the src folder , if using eclipse. Let me know if any issues, I will helpe. If the answer helped, please do rate it. Thank you.

To indent code in eclipse, select code by pressing Ctrl+A and then Ctrl+i


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class UPCChecksum {
private static void validate(String upc_in)
{
int oddSum = 0;
int evenSum = 0;
long upc = Long.parseLong(upc_in);
int checksumFound = (int)(upc % 10);//extract the last digit
//drop the last digit
upc /= 10;
for(int i = 11; i >= 1; i--)
{
int digit = (int)(upc % 10);
if(i % 2 == 0) //even position
evenSum += digit;
else
oddSum += digit;
upc /= 10;
}
int value = 3 * oddSum + evenSum;
int remainder = value % 10;
int calculatedCheckSum;
if(remainder == 0)
calculatedCheckSum = 0;
else
calculatedCheckSum = 10 - remainder;
if(calculatedCheckSum == checksumFound)
System.out.println(upc_in + " valid");
else
System.out.println(upc_in + " invalid, correct checksum = " + calculatedCheckSum);
}
public static void main(String[] args) {

if(args.length != 1)
{
System.out.println("Usage: java UPCChecksum <inputfilename>");
return;
}
try {
Scanner infile = new Scanner(new File(args[0]));
while(infile.hasNext())
{
String upc = infile.next();
validate(upc);
}
infile.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}

sample input file: upc.txt

079400804501
024000162860
011110856807
051000138101

output

079400804501 valid
024000162860 invalid, correct checksum = 5
011110856807 valid
051000138101 valid

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote