ISBN2.java


Below is the syntax highlighted version of ISBN2.java from §1.3 Conditionals and Loops.


/******************************************************************************
 *  Compilation:  javac ISBN2.java
 *  Execution:    java ISBN2 n
 *
 *  Determines the check digit of an ISBN number given the first 9 digits.
 *  Prints out the fully formatted ISBN number.
 *
 *  An ISBN number is legal if it consists of 10 digits and
 *  d_1 + 2*d_2 + 3*d_3 + ... + 10*d_10 is a multiple of 11.
 *  For example, 0-201-31452-5 is legal since
 *  1*5 + 2*2 + 3*5 + 4*4 + 5*1 + 6*3 + 7*1 + 8*0 + 9*2 + 10*0 = 88
 *  and 88 is a multiple of 11.
 *
 *  %  java ISBN2 020131453
 *  0-201-31453-3
 *
 *  % java ISBN2 020131454
 *  0-201-31454-1
 *
 *  % java ISBN2 020131455
 *  0-201-31455-X
 *
 ******************************************************************************/

public class ISBN2 {

    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);
        String isbn = "";

        int sum = 0;
        for (int i = 2; i <= 10; i++) {
            int digit = n % 10;
            if (i == 2 || i == 7 || i == 10) isbn = "-" + isbn;
            isbn = digit + isbn;
            sum += i * digit;
            n = n / 10;
        }

        // print out check digit, use X for 10
        if (sum % 11 == 1)      isbn += "X";
        else if (sum % 11 == 0) isbn += "0";
        else                    isbn += (11 - sum % 11);

        System.out.println(isbn);
    }
}


Copyright © 2000–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:12:31 EDT 2022.