www.webdeveloper.com
Results 1 to 6 of 6

Thread: ISBN check

  1. #1
    Join Date
    Nov 2003
    Posts
    18

    ISBN check

    Can sum1 help me cos im writing this code which will allow me to check if any ISBN I input via keyboard is correct.

    Currently iv managed to get the code which allows me to input from the keyboard and im stuck i pretty clueless bout this.

    Can sum1 pls help! Thanx in advance!

  2. #2
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    You want to check if a string is a valid ISBN number in Java? Are you sure you're not getting confused with JavaScript?
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  3. #3
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    If you are indeed looking for java, and not javascript, you could use this (based on php code found at http://www.planet-source-code.com/vb...Id=20&lngWId=8)
    Code:
    public class ISBN {
      public static boolean isValid(String ISBN) {
        String theISBN=ISBN.replaceAll("[^\\dX]", "");
        if(theISBN.length()!=10) return false;
        int checkDigit=11;
        for(int i=2; i<=10; i++) {
          checkDigit+=i*(((int)theISBN.charAt(10-i))-48);
        }
        checkDigit=(11-(checkDigit%11));
        char check=(char)(checkDigit+48);
        if(checkDigit==10) check='X';
        else if(checkDigit==11) check='0';
    
        if(theISBN.charAt(9)==check) return true;
        return false;
      }
    }
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  4. #4
    Join Date
    Nov 2003
    Posts
    18
    Hmm i was told that u had to parse the string into an integer before using it the calculation of the checksum digit.

    Anyone else got any ideas?

  5. #5
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    In a way, I am parsing the string into an integer; in ascii, the number characters 0-9 have character codes 48-57, so by using (int)charAt(i)-48, I obtain the integer value of that character. I've tried my code on some ISBNs found on amazon.com, and then on some random permutations of numbers. It seems to work ok (ie return true for real ones and false for fake ones)
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  6. #6
    Join Date
    Nov 2003
    Posts
    18
    alrite thanx

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles