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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java UPC File

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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
       {
           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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Conversion of any image file to text file in Java...'
    By suyog53 in forum File I/O & Other I/O Streams
    Replies: 17
    Last Post: September 23rd, 2012, 08:37 AM
  2. Java I/O File code; how to read/write file
    By ryu2le in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 18th, 2011, 05:51 PM
  3. Replies: 10
    Last Post: January 12th, 2011, 05:48 AM
  4. How do you convert a jar file into a java file, how ?
    By auto78900 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 12th, 2010, 12:32 AM
  5. insert(embed) a file object (.txt file) in MS excel sheet using java.
    By jyoti.dce in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 12th, 2010, 08:16 AM