Eugene Pi
Eugene Pi

Reputation: 45

Java ISBN checker/calculator, how to solve?

Im doing my homework and literally stuck for 5 hours for this particular question.

The last digit d10 is a checksum, which is calculated from the other nine digits using the following formula: (d1*1 + d2*2 + d3*3+ d4*4 + d5*5 + d6*6 + d7*7 + d8*8 + d9*9) % 11; If the checksum is 10, the last digit is denoted X according to the ISBN convention.

Write a program that prompts the user to enter the first 9 digits and displays the 10-digit ISBN (including leading zeros). Your program should read the input as an integer. For example, if you enter 013601267, the program should display 0136012671. If the user omits the leading zeroes, the program should continue by adding the leading zeroes. For example, if you enter 12345, the program should display "The correct ISBN number is 0000123455".

I used for loops, switch and everything but with my knowledge, I couldn't solve it. I can calculate d10 but the problem is.. 1) I don't understand how 013601267 can be calculated because it's not 0 1 3 6 ... Because i am using scanner object, i have no idea how to proceed. 2) And I can't figure out how to add the leading zeroes.

If someone has an answer for this problem, or someone can advise me, plz help me!!

Upvotes: 0

Views: 1505

Answers (1)

ameed
ameed

Reputation: 1160

First, parse the input into a String.

Then, loop through the String and add each character (in int form, doing the necessary calculations) to a sum variable of type int. (Multiply by the index of each char + 1.)

Finally, do the modulus. Then, if the result equals 10, append an X to the ISBN string; otherwise, append the result of the modulus operation.

Edit: to add on the zeroes, count the number of characters in the String form of the ISBN, then add on that number of zeroes - 9 to the start of the ISBN.

Hope this helps!!

Upvotes: 2

Related Questions