# Thread: isbn checker

1. Member Join Date
Oct 2010
Posts
2
Rep Power
0

## isbn checker

I'm a newbie to java. Now I'm writing a code to validate international book standard code (ISBN) numbers. Those are 13 digits numbers. The numbers can only contain 13 digits, so no letters or symbols. Further more, the last digit in the number is sorta special. It's called the check number and it is calculated based on the previous 12 digits. There is a formula for it. I copied the next paragraph from wikipedia.

The calculation of an ISBN-13 check digit begins with the first 12 digits of the thirteen-digit ISBN (thus excluding the check digit itself). Each digit, from left to right, is alternately multiplied by 1 or 3, then those products are summed modulo 10 to give a value ranging from 0 to 9. Subtracted from 10, that leaves a result from 1 to 10. A zero (0) replaces a ten (10), so, in all cases, a single check digit results.

For example, the ISBN-13 check digit of 978-0-306-40615-? is calculated as follows:
s = 9×1 + 7×3 + 8×1 + 0×3 + 3×1 + 0×3 + 6×1 + 4×3 + 0×1 + 6×3 + 1×1 + 5×3
= 9 + 21 + 8 + 0 + 3 + 0 + 6 + 12 + 0 + 18 + 1 + 15
= 93
93 / 10 = 9 remainder 3
10  3 = 7

and here is the code I wrote. I let the users to first input a number and then check it.

public class isbnchecker {

public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("input an isbn");
String isbn = scan.nextLine();
isbnchecker main = new isbnchecker();
System.out.println(isbn + main.checkISBN(isbn));
}

private boolean checkISBN (String isbn){
char[] isbnChar = isbn.toCharArray();
//check if the length is 13
if (isbnChar.length != 13)
return false;
//check if only digits

for(int i=0;i<isbnChar.length;i++){
if (!Character.isDigit(isbnChar[i]))
return false;
}
//check if the checknumber is correct
int sum = 0;

for(int i=0;i<(isbnChar.length-1);i=i+2){
sum = sum + Character.getNumericValue(isbnChar[i]);
}
for (int i=1;i<isbnChar.length;i=i+2){
sum = sum + (Character.getNumericValue(isbnChar[i])*3);
}
int CheckDigit = 10-sum%10;
int LastDigit = Character.getNumericValue(isbnChar[isbnChar.length]);
if (CheckDigit != LastDigit)
return false;

return true;

}
}

But I can't run it. Whenever I inpu a 13 digit number, ecllipse says

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
at isbnchecker.checkISBN(isbnchecker.java:33)
at isbnchecker.main(isbnchecker.java:11)

Problems are due to check the check digit part. Idk why. I think the logic is quite clear.

Soooo plz help me. thanks in advance!!  Reply With Quote

2. Senior Member Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
12

## Your problem is here

Java Code:
`int LastDigit = Character.getNumericValue(isbnChar[isbnChar.length]);`
isbnChar[isbnChar.length] cannot be accessed since array indexing starts at 0. That means in array of 12 elements to access the last one you use isbn. 0 to 11 contains 12 elements. if you use isbn you are really looking for the 13th element (which does not exist) and an out of bounds exception is thrown.
So to solve your problem use isbnChar.length-1  Reply With Quote

3. Member Join Date
Oct 2010
Posts
2
Rep Power
0

## Oh right!!^^ Thank you very much!!  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•