An ISBN (International Standard Book Number) identifies a unique publication. An
ID: 3627481 • Letter: A
Question
An ISBN (International Standard Book Number) identifies a uniquepublication. An ISBN is ten digits. The first nine digits must be decimal digits
(0...9). The tenth digit can be either a decimal digit or the letter X. Three single
dashes may be between any of the characters. (i.e., an ISBN number may
either have no dashes or exactly three dashes). Also, an ISBN must not begin
or end with a dash, and sequential dashes are not allowed.
Some example ISBNs:
0-201-88337-6
0-13-117334-0
0821211315 (no dashes is ok)
1-57231-866-X
The last character of an ISBN number is a checksum. The checksum is
determined from the first 9 digits; it is computed by taking modulo 11 (the
remainder after dividing by 11) of the sum of each digit multiplied by its
position in the ISBN. The letter X corresponds to a value of 10.
Here are two ISBNs and the calculations that show how the check sum is
determined:
0-201-88337-6:(0*1 + 2*2 + 0*3 + 1*4 + 8*5 + 8*6 + 3*7 + 3*8 + 7*9)mod 11=6
1-57231-866-X:(1*1 + 5*2 + 7*3 + 2*4 + 3*5 + 1*6 + 8*7 + 6*8 + 6*9)mod
11=10(X)
For more info, check out: www.isbn.org.
Some invalid ISBNs:
0-201-8A337-6 (bad digit)
0-201-88337-63 (too many digits)
0-201-88-337-6 (too many dashes)
0-201883376 (not enough dashes)
0-201-88337-3 (wrong check sum)
-013-117334-0 (beginning or ending dash)
157231--866-X (sequential dashes)
013-1134-0 (too few digits)
Write a Java program that will validate ISBNs. For each ISBN, the program
should state if it is a valid or invalid ISBN, along with the appropriate error message specifying the reason why that particular ISBN is invalid, if invalid.
The ISBN numbers are entered by user and your program should run as long
as the user enters ISBN numbers to validate. Exit when user enters "q" or
Explanation / Answer
import java.util.*;
public class ValidateISBN {
public static String validate(String isbn) {
char lastChar = isbn.charAt(isbn.length()-1);
if (isbn.charAt(0) == '-' || lastChar == '-') return "Beginning or ending dash";
if (isbn.contains("--")) return "Sequential dashes";
int dashCount = 0;
int numCount = 0;
int checkSum = 0;
for (int i=0; i<isbn.length()-1; i++) {
if (isbn.charAt(i) == '-') {
dashCount++;
} else if (Character.isDigit(isbn.charAt(i))) {
numCount++;
checkSum += numCount * Character.digit(isbn.charAt(i),10);
} else return "Bad digit";
}
if (dashCount != 0 && dashCount < 3) return "Too few dashes";
if (dashCount > 3) return "Too many dashes";
if (numCount < 9) return "Too few digits";
if (numCount > 9) return "Too many digits";
int checkSumDigit;
if (lastChar == 'X') {
checkSumDigit = 10;
} else if (Character.isDigit(lastChar)) {
checkSumDigit = Character.digit(lastChar,10);
} else {
return "Bad digit";
}
if (checkSum % 11 != checkSumDigit) return "Wrong check sum";
return "Valid ISBN";
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true) {
System.out.print("Enter an ISBN (or q to quit): ");
String isbn = sc.nextLine();
if (isbn.equals("q")) break;
System.out.println("Result of test: "+validate(isbn));
}
}
}