Search code examples
javaloopsobjectisbn

Checksums - ISBN program


This problem has me puzzled. I tried using a loop like this: Basically I tried to get the first digit from the input and do the formula but it doesn't seem to work. It looks so simple but I can't figure it out. Could you help me? Thanks.

public static int ISBN(String ninedigitNum) {
   number = 9;
   while (number > 0) {
   int nextDigit = ninedigitNum.substring(0,1);

   ...
}

Checksums (Source: Princeton University). The International Standard Book Number (ISBN) is a 10 digit code that uniquely specifies a book. The rightmost digit is a checksum digit which can be uniquely determined from the other 9 digits from the condition that d1 + 2d2 + 3d3 + ... + 10d10 must be a multiple of 11 (here di denotes the ith digit from the right). The checksum digit d1 can be any value from 0 to 10: the ISBN convention is to use the value X to denote 10. Example: the checksum digit corresponding to 020131452 is 5 since is the only value of d1 between 0 and and 10 for which d1 + 2*2 + 3*5 + 4*4 + 5*1 + 6*3 + 7*1 + 8*0 + 9*2 + 10*0 is a multiple of 11. Create a Java method ISBN() that takes a 9-digit integer as input, computes the checksum, and returns the 10-digit ISBN number. Create 3 JUnit test cases to test your method.

I got it, thanks a lot everyone!


Solution

  • What about it isn't working? Either way, I believe what you're missing is that you're continually getting the same substring, which will be the first number of the string: int nextDigit = ninedigitNum.substring(0,1);. In addition, you're going to want to use an int, not a String; you can technically convert from String to int if desired, but the problem itself calls for an int.

    There are two ways to do this that jump to mind. I would do this by realizing that mod in powers of 10 will give you the respective digit of an integer, but the easier way is to convert to a char array and then access directly. Note that there's no error checking here; you'll have to add that yourself. In addition, there are a LOT of 'magic numbers' here: good code typically has very, very few. I would recommend learning more data structures before attempting problems like these; to be honest there's very few things you can do without at least arrays and linked lists.

    char[] ISBN = ninedigitNum.toCharArray();
    
    //Process each number
    int total = 0;
    for(int i=0; i<9; i++){
        int current_int = Integer.parseInt(ISBN[i]);
        total += current_int * (10 - i)
    }
    
    //Find value of d1
    for(int i=0; i<9; i++){
        if(((total + i) % 11) == 0){
            total += i*100000000;
            break;
        }
    }
    
    return total;
    

    In general: Use print outs with System.out.println(x); or use your compiler's debugger to see what's going on during processing.