Results 1 to 3 of 3
Thread: isbn checker
- 10-27-2010, 06:25 PM #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!!
- 10-27-2010, 07:18 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Your problem is here
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[11]. 0 to 11 contains 12 elements. if you use isbn[12] you are really looking for the 13th element (which does not exist) and an out of bounds exception is thrown.Java Code:int LastDigit = Character.getNumericValue(isbnChar[isbnChar.length]);
So to solve your problem use isbnChar.length-1
- 10-27-2010, 07:31 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Java program: ISBN calculated by other 9 digits?
By SNFA in forum New To JavaReplies: 5Last Post: 10-09-2010, 05:22 AM -
Spell Checker in Swing
By dhaivat in forum New To JavaReplies: 2Last Post: 07-01-2010, 05:08 PM -
Grammar Checker
By zaz_rin in forum New To JavaReplies: 1Last Post: 07-19-2009, 02:01 PM -
ISBN Checker
By blobs86 in forum New To JavaReplies: 1Last Post: 03-23-2009, 11:07 PM -
Formatting isbn number with Math.random()
By dns77x7 in forum New To JavaReplies: 11Last Post: 09-21-2008, 06:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks