ISBNs are often handled manually and there is therefore a need for a quick
way to check whether a particular ISBN is likely to be valid or not.
A typical ISBN is represented below, starting with a country code (
), followed by a publisher code (
) and a title code (
). The tenth digit (
; called check-digit) depends on all the others. So if there is an alteration in one or more of the first nine digits—and the check-digit is re-calculated, its value is very likely to be different from its original value.
where
public class Mod11Ck {
public static String calc(String digStr) {
int len = digStr.length();
int sum = 0, rem = 0;
int[] digArr = new int[len];
for (int k=1; k<=len; k++) // compute weighted sum
sum += (11 - k) * Character.getNumericValue(digStr.charAt(k - 1));
if ((rem = sum % 11) == 0) return "0";
else if (rem == 1) return "X";
return (new Integer(11 - rem)).toString();
}
public static void main(String[] args) {
System.out.println(args[0]+"-"+calc(args[0]));
}
}