Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Algorithm to find out checkdigits

807600Nov 9 2007 — edited Nov 13 2007
I'm working on a code that finds the checkdigit of an array of ISBN numbers, and returns a boolean value of true is the ISBN number is valid and false if the ISBN nemuber is invalid. I've figured out most of it but need help with figuring out the rest. The whole thing takes up about three pages, here are screenshots of it in order: http://img219.imageshack.us/img219/1024/compscilab74iyt5.jpg

http://img233.imageshack.us/img233/4888/compscilab74iisj7.jpg

http://img218.imageshack.us/img218/7261/compscilab74iiirs3.jpg

If you don't want to go to the links, I'll try to explain it:

International Standard Book Numbers

The final character of a ten-digit International Standard Book Number (ISBN) is a check digit that is computed as follows:

Working from right to left,

multiply each digit (other than the as-yet-unknown check digit) by its position in the number (including the as-yet-unknown check digit);

add the products together;

subtract the total from the closest multiple of 11 greater than or equal to that total. The answer is the check digit (using the letter "X" if the answer happens to be 10).

Consider, for example, the ISBN 0-201-53082-1.

Calculating the digit-by-position products yields

2 ? 2 = 4; 8 ? 3 = 24; 0 ? 4 = 0; 3 ? 5 = 15; 5 ? 6 = 30;
1 ? 7 = 7; 0 ? 8 = 0; 2 ? 9 = 18; 0 ? 10 = 0.

Adding the products together yields

4 + 24 + 0 + 15 + 30 + 7 + 0 + 18 + 0 = 98.

Subtracting 98 from 99 yields the check digit 1.

Since our calculation yields the same result as the final digit of the given ISBN, we conclude that this ISBN is valid.

While this may seem more complicated than UPC check digits, it can be validated very simply by adding together all the digit-by-position products (including the check digit, whose position is of course 1). If the result is a multiple of 11, then the ISBN is valid. (Recall that to check if an int n is a multiple of 11, determine whether or not n % 11 is 0.)


The String Array c contains the "stringified" individual elements (including hypens) of a ten-digit ISBN. (In the above example, the contents of c would be "0", "-", "2", "0", "1", "-", "5", "3", "0", "8", "2", "-", "1", in that order.) Complete the following code to store in the boolean variable good the value true if and only if the ISBN is valid.

// Enter values to test here
String[] c = { "0", "-", "2", "0", "1", "-", "5", "3", "0", "8", "2", "-", "1" };
boolean good;

int sum = 0;
int pos = 1;

// Loop over array from end to start
for ( int i = c.length - 1; i >= 0; i-- )
{
// Ignore hyphens
if ( !c.equals( "-" ) )
{
// Deal with a possible "X" digit
if ( sum > 0 )
sum += Integer.parseInt(c[i]) * pos;
else
sum += Integer.parseInt(c[i]) * pos;

pos++;
}
}
int j = sum;
while(j % 11 != 0)
j++;
System.out.println(sum);
System.out.println(j);

good = ((j - sum) == Integer.parseInt(c[12]));

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 11 2007
Added on Nov 9 2007
13 comments
2,104 views