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.

有帮助吗?

解决方案

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;

其他提示

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$)))

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;

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

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));
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top