Results 1 to 3 of 3

Thread: isbn checker

  1. #1
    jingliu316 is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default 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 = 91 + 73 + 81 + 03 + 31 + 03 + 61 + 43 + 01 + 63 + 11 + 53
    = 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!!

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    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[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.
    So to solve your problem use isbnChar.length-1

  3. #3
    jingliu316 is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Replies: 5
    Last Post: 10-09-2010, 05:22 AM
  2. Spell Checker in Swing
    By dhaivat in forum New To Java
    Replies: 2
    Last Post: 07-01-2010, 05:08 PM
  3. Grammar Checker
    By zaz_rin in forum New To Java
    Replies: 1
    Last Post: 07-19-2009, 02:01 PM
  4. ISBN Checker
    By blobs86 in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 11:07 PM
  5. Formatting isbn number with Math.random()
    By dns77x7 in forum New To Java
    Replies: 11
    Last Post: 09-21-2008, 06:02 PM

Posting Permissions

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