Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a2c6473

Browse files
adding ISBN 13 validator
1 parent 4aced19 commit a2c6473

File tree

1 file changed

+37
-0
lines changed
  • api-first-development-service/src/main/java/com/csaba79coder/apifirstdevelopment/util

1 file changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.csaba79coder.apifirstdevelopment.util;
2+
3+
public class ISBN13Validator {
4+
5+
public static boolean validISBN(Long isbnLong) {
6+
String isbn = String.valueOf(isbnLong);
7+
if (isbn == null) {
8+
return false;
9+
}
10+
if (isbn.startsWith("-")) {
11+
return false;
12+
}
13+
isbn = isbn.replaceAll( "-", "" );
14+
if ( isbn.length() != 13 ) {
15+
return false;
16+
}
17+
try {
18+
int tot = 0;
19+
for (int i = 0; i < 12; i++) {
20+
int digit = Integer.parseInt( isbn.substring( i, i + 1 ) );
21+
tot += (i % 2 == 0) ? digit : digit * 3;
22+
}
23+
int checksum = 10 - (tot % 10);
24+
if ( checksum == 10 ) {
25+
checksum = 0;
26+
}
27+
return checksum == Integer.parseInt( isbn.substring( 12 ) );
28+
}
29+
catch (NumberFormatException nfe) {
30+
return false;
31+
}
32+
}
33+
34+
private ISBN13Validator() {
35+
36+
}
37+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /