Java File UPC

Page 1 of 1

4 Replies - 2333 Views - Last Post: 06 December 2012 - 05:27 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=302890&amp;s=7976f8c03bdc234da799f8cfae5c746f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 ThatBeast

Reputation: 2
• Posts: 38
• Joined: 16-November 12

Java File UPC

Posted 05 December 2012 - 04:31 PM

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.

Is This A Good Question/Topic? 0

Replies To: Java File UPC

#2 g00se

• D.I.C Lover

Reputation: 3225
• Posts: 14,227
• Joined: 20-September 08

Re: Java File UPC

Posted 05 December 2012 - 04:40 PM

Quote

while(numScan.hasNextInt())
should be hasNextLine, as the line must be read as a String
Have a look at this (which is similar)

http://technojeeves....hn-algo-in-java

#3 ThatBeast

Reputation: 2
• Posts: 38
• Joined: 16-November 12

Re: Java File UPC

Posted 05 December 2012 - 05:52 PM

g00se, on 05 December 2012 - 04:40 PM, said:

Quote

while(numScan.hasNextInt())
should be hasNextLine, as the line must be read as a String
Have a look at this (which is similar)

http://technojeeves....hn-algo-in-java

But what can I do to finish the assignment?

#4 ThatBeast

Reputation: 2
• Posts: 38
• Joined: 16-November 12

Re: Java File UPC

Posted 06 December 2012 - 03:17 PM

Does anybody know what I could do to finish this assignment?

#5 g00se

• D.I.C Lover

Reputation: 3225
• Posts: 14,227
• Joined: 20-September 08

Re: Java File UPC

Posted 06 December 2012 - 05:27 PM

Quote

But what can I do to finish the assignment?