检查 Java 中的字符串是否遵循 ISBN-13

发布于 2024-12-13 11:31:19 字数 458 浏览 8 评论 0原文

我试图根据 ISBN- 的规则检查我读取的字符串(重要的是它是字符串)是否正确13.。我找到了一个公式

例如978-0-306-40615-的ISBN-13校验位?

计算如下:

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`

我的问题是我不知道如何将一个数字与 1 相乘,并将每个数字与 3 相乘?我猜是一个 for 循环,但我不知道如何开始。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凉宸 2024-12-20 11:31:19

您可以“简单地”使用正则表达式:

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

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

明媚如初 2024-12-20 11:31:19

您有 6 对(偶数、奇数)数字,因此请两两配对。

    for (i = 0; i < 6; i++) {
    even += array[2*i];
    odd += array[2*i+1]*3;
    }
    checkbit = 10 - (even+odd)%10;

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;
没企图 2024-12-20 11:31:19

假设您的输入字符串是 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;

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;
请恋爱 2024-12-20 11:31:19

这似乎有效且明确。

// 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');
}

注意:经过编辑以正确处理 0 校验位。请参阅维基百科国际标准书号,例如校验位为 0 的 isbn。

Paul

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

好多鱼好多余 2024-12-20 11:31:19

类似的,使用循环和可怕的字符到字符串到整数的转换;]

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));
}

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));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文