2

Im trying to check if a string (important that it is a string) that im reading is correct accoring to the rules of ISBN-13. I found a formula

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`

My problem is i don't know how to multiply one number with 1 and every other with 3 ? Im guessing a for-loop but i don't know how to start.

Rocksteady
  • 271
  • 1
  • 4
  • 21

5 Answers5

5

You could "simply" use regular expressions:

ISBN(-1(?:(0)|3))?:?\x20+(?(1)(?(2)(?:(?=.{13}$)\d{1,5}([ -])\d{1,7}\3\d{1,6}\3(?:\d|x)$)|(?:(?=.{17}$)97(?:8|9)([ -])\d{1,5}\4\d{1,7}\4\d{1,6}\4\d$))|(?(.{13}$)(?:\d{1,5}([ -])\d{1,7}\5\d{1,6}\5(?:\d|x)$)|(?:(?=.{17}$)97(?:8|9)([ -])\d{1,5}\6\d{1,7}\6\d{1,6}\6\d$)))

dwalldorf
  • 1,379
  • 3
  • 12
  • 21
  • Have not tried this expression but regular expressions do work in Java :) Maybe you take a look at this for better understanding: http://download.oracle.com/javase/tutorial/essential/regex/ – dwalldorf Nov 03 '11 at 12:15
  • Yes, but you have to escape the escaping \, so \d will be \\d for example. Read about [Pattern](http://download.oracle.com/javase/1,5.0/docs/api/java/util/regex/Pattern.html) and [Matcher](http://download.oracle.com/javase/1,5.0/docs/api/java/util/regex/Matcher.html) – zeller Nov 03 '11 at 12:16
  • LOL, nice one, but DONT do this;] Regexps are cool, but this is NOT the task for them ;] – Adam Jurczyk Nov 03 '11 at 12:18
  • @AdamJurczyk: Why should this be not the right task for regular expressions?! – dwalldorf Nov 03 '11 at 12:33
  • 1
    well... look at this regexp^^ its awful, long, unreadable and completely unmaintainable ^^ – Adam Jurczyk Nov 03 '11 at 12:47
  • @AdamJurczyk: Just as they always are ;) Anyway, regular expressions are for recognizing patterns in strings, which this one does. Btw There are regex, that are much more complicated then this one :) – dwalldorf Nov 03 '11 at 14:20
4

You have 6 pairs of (even,odd) numbers, so go through them pairwise.

    for (i = 0; i < 6; i++) {
    even += array[2*i];
    odd += array[2*i+1]*3;
    }
    checkbit = 10 - (even+odd)%10;
rsj
  • 788
  • 3
  • 10
  • This is what i was thinking about! But why is it [2*i+1]? i was thinking i should be [i+1]? – Rocksteady Nov 03 '11 at 12:21
  • even numbers are 2*i, odd numbers are 2*i + 1. Just think what is the difference between 3 and the next odd number, 5. They differ by 2. – rsj Nov 03 '11 at 12:24
  • Souldn't that be 'checkbit = (10 - (even + odd) % 10) % 10;' otherwise you may return 10 instead of 0 – OldCurmudgeon Nov 04 '11 at 13:07
  • Yes! The referenced doc gives the alg. I gave, but then elsewhere they say "10 is interpreted as 0". LOL. – rsj Nov 04 '11 at 21:46
1

assuming your inputString is ascii:

    int odd = 0;
    int even = 0;
    char[] c = (inputString + "00").replaceAll("[\\-]", "").toCharArray();
    for (int i = 0; i < (c.length - 1) / 2; ++i) {
        odd += c[2 * i] - 48;
        even += c[2 * i + 1] - 48;
    }
    int result = 10 - (odd + 3 * even) % 10;
soulcheck
  • 36,297
  • 6
  • 91
  • 90
1

This seems to work effectively and is clear.

// Calculates the isbn13 check digit for the 1st 12 digits in the string.
private char isbn13CheckDigit(String str) {
  // Sum of the 12 digits.
  int sum = 0;
  // Digits counted.
  int digits = 0;
  // Start multiplier at 1. Alternates between 1 and 3.
  int multiplier = 1;
  // Treat just the 1st 12 digits of the string.
  for (int i = 0; i < str.length() && digits < 12; i++) {
    // Pull out that character.
    char c = str.charAt(i);
    // Is it a digit?
    if ('0' <= c && c <= '9') {
      // Keep the sum.
      sum += multiplier * (c - '0');
      // Flip multiplier between 1 and 3 by flipping the 2^1 bit.
      multiplier ^= 2;
      // Count the digits.
      digits += 1;
    }
  }
  // What is the check digit?
  int checkDigit = (10 - (sum % 10)) % 10;
  // Give it back to them in character form.
  return (char) (checkDigit + '0');
}

NB: Edited to correctly handle the 0 check digit. See Wikipedia International Standard Book Number for example isbn with check digit of 0.

Paul

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • I am a little concerned that (sum % 10) could be 0 making 10 - 0 = 10 and therefore the return would be ':'. Perhaps the algorithm ensures that this is not possible. – OldCurmudgeon Nov 03 '11 at 15:23
0

Similar, with loop and awful char-to-string-to-int conversions ;]

boolean isISBN13(String s){
    String ss = s.replaceAll("[^\\d]", "");
    if(ss.length()!=13)
        return false;
    int sum=0, multi=1;
    for(int i=0; i<ss.length()-1; ++i){
        sum += multi * Integer.parseInt(String.valueOf(ss.charAt(i)));
        multi = (multi+2)%4; //1 or 3
    }
    return (Integer.parseInt(String.valueOf(ss.charAt(ss.length()))) == (10 - sum%10));
}
Adam Jurczyk
  • 2,153
  • 12
  • 16