Forums Register Login

read and validate a 12-digit barcode

+Pie Number of slices to send: Send
Hello, I am new to this forum and to Java, been taking a class for just about a month now.
I have this assignment and don't know how to continue it.

Write a program that prompts for and reads a 12-digit barcode and then validates the barcode as described by the algorithm: we dissect a barcode into d0,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10, and d11 the scanned check digit.

1. Determine z; the calculated digit as follows: x= 3*(d0+d2+d4+d6+d8+d10)+(d1+d3+d5+d7+d9)
2. y= the last digit in x
3. if y = 0, then z=0; otherwise, z= 10-y
4. If d11=z then upc is valid

1
+Pie Number of slices to send: Send
Welcome to the Ranch!

While we understand how some of these things can be confusing for beginners, you're probably going to need to put in more effort in coming up with code that implements that algorithm. It's fairly straightforward and I'm sure your course materials contain information on what operations (assignment, calculation, conditional, iteration) you'll need for this. Please go back and review the lessons you've had so far. We have a policy here at the ranch to not do students' homework for them. We'll only give you tips and advice on how to get unstuck or do something better.

Good luck.

EDIT: If you're still stuck after reviewing your notes and lessons, post a list of the topics you've already covered in your classes and labs so far so we have an idea of what you have to work with.
+Pie Number of slices to send: Send
 

Junilu Lacar wrote:Welcome to the Ranch!

While we understand how some of these things can be confusing for beginners, you're probably going to need to put in more effort in coming up with code that implements that algorithm. It's fairly straightforward and I'm sure your course materials contain information on what operations (assignment, calculation, conditional, iteration) you'll need for this. Please go back and review the lessons you've had so far. We have a policy here at the ranch to not do students' homework for them. We'll only give you tips and advice on how to get unstuck or do something better.

Good luck.

EDIT: If you're still stuck after reviewing your notes and lessons, post a list of the topics you've already covered in your classes and labs so far so we have an idea of what you have to work with.



Like i said im very new to java, I was just wondering is this the right way of doing it? Or should i input the barcode as a string, not as a number since it could technically have leading zeros. And then do d0-d11 with substring?
1
+Pie Number of slices to send: Send
 

Michael rust wrote:
Like i said im very new to java, I was just wondering is this the right way of doing it?



I am assuming that this is a trick question, as you probably already tried it, and it didn't work as you wanted?

Michael rust wrote:Or should i input the barcode as a string, not as a number since it could technically have leading zeros. And then do d0-d11 with substring?



Sure. Why not? Sometimes it is better to just experiment (ie. just do it, as Nike states), than to wait for the perfect algorithm.

Henry
+Pie Number of slices to send: Send
You can't get a barcode into an int because an int can't accommodate more than ten digits. You would have to enter it as a String. You can use a regular expression to verify it has the correct format, but that might be an advanced problem. You are right that the leading 0s will make it impossible to implement as a number.
There are simpler ways to get an individual char out of a String than substring. Have a look through the methods of String. While we are looking for methods consider looking through the Character class' methods for something to turn a letter into a number. But do you need it as a number at all? Are barcodes numbers? If they are, then you can do arithmetic with them. I have never heard of anybody trying barcode arithmetic.
+Pie Number of slices to send: Send
 

Michael rust wrote:Or should i input the barcode as a string, not as a number since it could technically have leading zeros. And then do d0-d11 with substring?



Sure. Why not? Sometimes it is better to just experiment (ie. just do it, as Nike states), than to wait for the perfect algorithm.

Henry



I am using this to make sure the lenght of my barcode is 12. But it is giving me this error:  cannot find symbol
if (barcode.lenght()!=12)
          ^
 symbol:   method lenght()
 location: variable barcode of type String

But my teacher used this exact same barcode.lenght method in class and it worked.
+Pie Number of slices to send: Send
It is pointing to the '.'
1
+Pie Number of slices to send: Send
Wow my mistake i just realized i misspelled length
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:You can't get a barcode into an int because an int can't accommodate more than ten digits. You would have to enter it as a String. You can use a regular expression to verify it has the correct format, but that might be an advanced problem. You are right that the leading 0s will make it impossible to implement as a number.
There are simpler ways to get an individual char out of a String than substring. Have a look through the methods of String. While we are looking for methods consider looking through the Character class' methods for something to turn a letter into a number. But do you need it as a number at all? Are barcodes numbers? If they are, then you can do arithmetic with them. I have never heard of anybody trying barcode arithmetic.



I see, I would be able to use the charAt method. This is the only one i learned in class thus far
+Pie Number of slices to send: Send
Yes, charAt is what I was thinking of.
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1505 times.
Similar Threads
Java project help - UPC calculator/toString?
Garbage Collection
ID number errors
Any help would be appreciated
I need to a call a friend. That means YOU! help!
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:22:35.