1 public boolean validateIsbn10( String isbn )
2 {
3 if ( isbn == null )
4 {
5 return false;
6 }
7
8 //remove any hyphens
9 isbn = isbn.replaceAll( "-", "" );
10
11 //must be a 10 digit ISBN
12 if ( isbn.length() != 10 )
13 {
14 return false;
15 }
16
17 try
18 {
19 int tot = 0;
20 for ( int i = 0; i < 9; i++ )
21 {
22 int digit = Integer.parseInt( isbn.substring( i, i + 1 ) );
23 tot += ((10 - i) * digit);
24 }
25
26 String checksum = Integer.toString( (11 - (tot % 11)) % 11 );
27 if ( "10".equals( checksum ) )
28 {
29 checksum = "X";
30 }
31
32 return checksum.equals( isbn.substring( 9 ) );
33 }
34 catch ( NumberFormatException nfe )
35 {
36 //to catch invalid ISBNs that have non-numeric characters in them
37 return false;
38 }
39 }
40