4 Replies - 417 Views - Last Post: 06 December 2012 - 05:27 PM Rate Topic: -----

#1 ThatBeast  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • 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
   {
       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.

Is This A Good Question/Topic? 0
  • +

Replies To: Java File UPC

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 ThatBeast  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 16-November 12

Re: Java File UPC

Posted 05 December 2012 - 05:52 PM

View Postg00se, 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?
Was This Post Helpful? 0
  • +
  • -

#4 ThatBeast  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • 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?
I've already made a comment
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1