# Java UPC File

• December 6th, 2012, 06:14 PM
ThatBeast
Java UPC File
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:

Code :

```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.
• December 6th, 2012, 06:35 PM
Norm
Re: Java UPC File
What is the output from your program?

I don't see where the posted code follows the algorithm.
The instructions for the program seem pretty clear. Which steps of the instructions have you done so far?
Which do you still need to do?

One problem is the use of the Scanner class's nextInt() method. It does NOT read separate digits unless they are separated by spaces. You will need to read the data into a String, get a character(digit) and convert that character to a numeric value to be able to apply the formula.

I suggest you work on the technique for separating a String of digits into single numeric values first. When that works, Then move on to the next step.
• December 6th, 2012, 06:55 PM
ThatBeast
Re: Java UPC File
With my level of understanding of Java I don't think I can continue the code.. But I desperately need it...
• December 6th, 2012, 07:06 PM
Norm
Re: Java UPC File
Try working on the logic first, not the java code. What steps does the program need to do to solve the problem? Make a list of the steps and then try to write the code for the steps.

As a part of the program you need to: work on the technique for separating a String of digits into single numeric values. Look at the String class for methods to get one character at a time from the String.