使用Java计算ISBN号校验位

时间:2016-03-05 04:29:43

标签: java algorithm isbn

我试图计算任何ISBN-13号码的校验位,如果ISBN号有效或无效,我不会太担心,但我一直在尝试做是让代码工作。我对算法的解释有明显的缺陷,欢迎提出如何解决它的建议,但主要问题是接收的用户输入对于整数变量来说太大了,但我也想避免双值的小数。

我已经尝试使用BigDecimalBigNumber,但我没有足够的经验来完全理解它们。这是找到d13(校验位)的算法:10-(d1 + 3d2 + d3 + 3d4 + d5 + 3d6 + d7 + 3d8 + d9 + 3d10 + d11 + 3d12)%10。

我知道守则是一团糟。我已使用this website作为对我想要做的事情的参考,并且我一直使用此ISBN号作为我的做法:9780132130806

我的问题是如何打印没有小数的最终ISBN号,我怎样才能修复算法? (我也非常感谢有关帮助教授JOption的网站上的任何提示,因为这是我使用的首选方法,因为它看起来比使用扫描仪更清洁)

import javax.swing.JOptionPane;
import java.math.BigInteger;

public class ISBN
{
    //George Sayegh Calculate check Digit ISBN

    public static void main(String[] args)
    {
    //Define Variables
    double ISBN12, D1, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12 = 0, D13;
    double A = 100000000000L;
    double B = 10000000000L;
    double C = 1000000000;
    double D = 100000000;
    double E = 10000000;
    double F = 1000000;
    double G = 100000;
    double H = 10000;
    double I = 1000;
    double J = 100;
    double K = 10;
    double L = 1;

    //Get ISBN #
    String ISBN12text = JOptionPane.showInputDialog("Please enter the first 12 digits of your ISBN number");
    ISBN12 = Double.parseDouble(ISBN12text);

    //Calculate D1
    D1 = ((ISBN12 - (ISBN12 % A)) / A); 

    //Calculate D2
    D2 = ((ISBN12 - (ISBN12 % B)) / B);

    //Calculate D3
    D3 = ((ISBN12 - (ISBN12 % C)) / C);

    //Calculate D4
    D4 = ((ISBN12 - (ISBN12 % D)) / D);

    //Calculate D5
    D5 = ((ISBN12 - (ISBN12 % E)) / E);

    //Calculate D6
    D6 = ((ISBN12 - (ISBN12 % F)) / F);

    //Calculate D7
    D7 = ((ISBN12 - (ISBN12 % G)) / G);

    //Calculate D8
    D8 = ((ISBN12 - (ISBN12 % H)) / H);

    //Calculate D9
    D9 = ((ISBN12 - (ISBN12 % I)) / J);

    //Calculate D10
    D10 = ((ISBN12 - (ISBN12 % K)) / K);

    //Calculate D11
    D11 = ((ISBN12 - (ISBN12 % L)) / L);

    //Get D13
    D13 = 10 - (D1 + (3 * D2) + D3 + 3 * D4 + D5 + 3 * D6 + D7 + 3 * D8 + D9 + 3 * 10 + D11 + 3 * D12) % 10; 

    JOptionPane.showMessageDialog(null, D1 +""+ D2 +""+ D3 +""+ D4 +""+ D5 +""+ D6 +""+ D7 +""+ D8 +""+ D9 +""+ D10 +""+ D11 +""+ D12 +""+ 13);
    }
}     

2 个答案:

答案 0 :(得分:2)

这是一个基于this Wikipedia example的更简单的实现。它缺乏健全性检查 - 例如输入字符串是否是有效的ISBN13 ---但应该足以让你去。我希望它有所帮助。

请注意,输入是ISBN-13字符串,其中删除了校验位(例如978013213080而不是9780132130806);程序打印输出上的校验位;如果这不是你想要的,你应该能够修改它。

public class CheckISBN13 {
    /* We assume isbnString is a *valid* ISBN-13 with the check digit removed.  */
    public static int[] stringToInt(String isbnString) {
        int[] isbnInt = new int[12];
        int j = 0;
        for (int i = 0; i < isbnString.length(); ++i) {
            if (Character.isDigit(isbnString.charAt(i))) {
                isbnInt[j++] = Character.getNumericValue(isbnString.charAt(i));
            }
        }
        return isbnInt;
    }

    public static int getCheckDigit(int[] isbnInt) {
        int val = 0;
        for (int i = 0; i < isbnInt.length; ++i) {
            int coeff = (i % 2 == 0 ? 1 : 3);
            val += coeff * isbnInt[i];
        }
        return 10 - (val % 10);
    }

    public static void main(String[] args) {
        if (args.length < 1) {
            System.err.println("Usage: java CheckISBN13 978-0-306-40615");
            System.exit(-1);
        }
        String isbnString = args[0];
        int[] isbnInt = stringToInt(isbnString);
        System.out.println("Check digit: " + getCheckDigit(isbnInt));
    }
}

答案 1 :(得分:2)

尝试这种极其简单的算法(用Python编写,但转换为Java应该很容易)。希望能够充分解释这些评论。应转换为不超过10行(不包括空行以便于阅读)

#// Valid ISBN-12 string from user (12 numbers, doesn't matter what other characters)
isbn12 = '978-0-306-40615' #// Input it from the user, hardcoded Wikipedia example for testing
coeff = 1 #// Multiple used in calculating sum
sum = 0

for d in isbn12: #// Equivalent to for(char d : isbn12.toCharArray())
    #// this next block is just a tryParse or an isDigit check
    #// if the character is a digit, parse it to an int
    #// else continue to next iteration
    try:
        #// just reassigning the value because-
        #// Hey, this is Python!
        d = int(d)
    except ValueError:
        continue

    #// Add to sum after multiplying with coeff
    sum += d * coeff
    #// If coeff was 1, change to 3 and vice versa
    #// Equivalent to coeff == 1 ? 3 : 1 in Java
    coeff = 3 if coeff == 1 else 1

#// END FOR LOOP

#// Get positive number to be added to make ISBN divisible by 10
#// The extra % 10 at the end is for changing 10 to 0 without if
check_digit = (10 - sum % 10) % 10

(每条评论开头的额外//是让SO代码格式化认为它是评论)

相关问题