• Post Reply Bookmark Topic Watch Topic
  • New Topic

read and validate a 12-digit barcode  RSS feed

 
Michael rust
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael rust
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael rust
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael rust
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is pointing to the '.'
 
Michael rust
Greenhorn
Posts: 10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow my mistake i just realized i misspelled length
 
Michael rust
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, charAt is what I was thinking of.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!