# isbn checker

• 10-27-2010, 06:25 PM
jingliu316
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!!
• 10-27-2010, 07:18 PM
al_Marshy_1981
`int LastDigit = Character.getNumericValue(isbnChar[isbnChar.length]);`