Problem 2.

Create a new java file called Barcode.java that will check to see if a given barcode number is valid. Barcodes have a 12 digit number on them that is used to determine the validity of the code. This is done by summing the odd position digits (1,3,5,7,9 and 11), multiplying this number by 3, then add the sum of all the even position digits(0,2,4,6,8,10 and 12) to this sum. If this sum is divisible by 10, then the barcode is valid. An example using the UPC code 214786948344 is shown below.

1. Sum all of the odd position digits (1,3,5,7,9 and 11).
2 + 4 + 8 + 9 + 8 + 4 = 35

2. Multiply the sum from step 1 by 3.
35 * 3 = 105

3. Sum all of the even position digits (2,4,6,8,10 and 12).
1 + 7 + 6 + 4 + 3 + 4 = 25

4. Add the sums from step 2 and 3 together.
105 + 25 = 130

Because 130 is divisible by 10, the barcode is valid.

For your program, you pass in a 12-digit number (of type long) using the nextLong() method of the Scanner class. Then it should output the remainder of your sum % 10. If the remainder is 0, the barcode is valid. Some sample outputs are shown below.

Input a 12-digit barcode:
214786948344
The barcode is valid if the output is 0.
Output = 0.

Input a 12-digit barcode:
446789001235
The barcode is valid if the output is 0.
Output = 3.

Input a 12-digit barcode:
758392346117
The barcode is valid if the output is 0.
Output = 4.

Thank you very much!!!

Recommended Answers

All 10 Replies

Please help me with this code

You forgot to post your code.

sorry for not posting the code. i actually got stuck because i couldn't figure out how to select for only the numbers in the odd positions....

how to select for only the numbers in the odd positions

How are you selecting the numbers?
Do you know how to determine if your index is even or odd?

no i was confused about how to select for the 1st, 3rd, 5th number etc. i was trying to use charat(), but i guess that's only for the string class so i couldn't use it for a variable

Where is the data that you are trying to scan? In a String or in a int or ???

sorry i wasn't being very clear (i was using input from the keyboard), so far my code is

import java.util.Scanner;
public class Barcode
{
public static void main (String[] args)
{
Scanner keyboard = new Scanner(System.in);

System.out.println(“Input a 12-digit barcode:”);
long barcode = keyboard.nextLong();

----- now i'm trying to select for the 1st, 3rd, 5th etc numbers in the barcode

Can you read it as a String vs a long?
Or convert the long to a String?
Otherwise you will have to shift the number to the right digit by digit and remember them.

can i read the variable as a string if i'm typing in a sequence of numbers?
otherwise i could convert it but i'm not sure how

Try it and see how it works.
nextLine will get everything up to the Enter.
nextInt will get the next number which could be the first of several numbers separated by spaces that were typed on the same line before Enter was pressed.

i could convert it but i'm not sure how

Look at the Long class. It has methods for working with long variables.

thanks for your help norm:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.