Hi There,
I have an assignment due and I really need help. The assignment description is:
To check the validity of the code you need to analyze each digit in the code including the check digit. Assume that the first digit is in position zero, add the digits in the even positions and multiply the result by 3. Add this to the sum of the odd positioned digits. Only if the total is a multiple of 10 is the UPC number valid.
For example: Given the UPC code of 016499215511 the following summation would prove that the bar code is valid.
3 X (0 + 6 + 9 + 2 + 5 + 1) + 1 + 4 + 9 + 1 + 5 + 1 = 90/10 = 9
Here is my code so far:
import java.util.Scanner; import java.io.*; class UPC { public static void main(String args[]) { try { FileReader data = new FileReader("data.txt"); BufferedReader numBuffer = new BufferedReader(data); Scanner numScan = new Scanner(numBuffer); int evenNumb = 0; int oddNumb = 0; System.out.println("UPC Code Validity"); while(numScan.hasNextInt()) { int n = numScan.nextInt(); if(n%2 == 0) { evenNumb += n; } else { oddNumb += n; } } System.out.println(evenNumb); numScan.close(); } catch (Exception e) { System.err.println("Error"); } } }
Data.txt looks like this:
016499215511
372415613274
155512994610
011165459192
838241762110
And the output should look like this:
UPC code Validity
016499215511 Valid
UPC code Validity
372415613274 Invalid
UPC code Validity
155512994610 Valid
UPC code Validity
011165459192 Invalid
UPC code Validity
838241762110 Invalid
Any help would be fantastic.