Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## 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:

```import java.util.Scanner;
import java.io.*;
class UPC
{
public static void main(String args[])
{
try
{
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.

2. ## 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.

3. ## 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...

4. ## 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.