Java ISBN 10位-13位互转工具类

话不多说,直接上代码:

 1 public class ISBNtool {
 2 
 3     public static String Convert(String isbn) throws Exception {
 4         isbn=isbn.replace("-","").replace(".","");
 5         if (isbn.length()==10){
 6             isbn=isbn.substring(0,9);
 7             isbn="978"+isbn;
 8             Integer a=0;
 9             Integer b=0;
10             for (int i = 1; i < isbn.length(); i+=2) {
11                 a+=Integer.parseInt(isbn.substring(i,i+1));
12                b+=Integer.parseInt(isbn.substring((i-1),i));
13             }
14 isbn=isbn+(10-(a*3+b)%10);
15 System.out.println("13isbn:"+isbn); 16 }else if (isbn.length()==13){
17 isbn=isbn.substring(3,isbn.length()-1);
18    Integer last=0;
19         for (int i = 0; i < isbn.length(); i++) { 20   last+=Integer.parseInt(isbn.substring(i,i+1))*(10-i);
21 }
22 last=11-(last%11);
23 if (last<=9)
24   isbn+=last; 25 else
26   isbn+="X";
27 System.out.println("10isbn:"+isbn);
28 }else{ 29 throw new Exception("ISBN格式不正确!");
30 }
31       return isbn; 32 }
33 }
34

 

posted @ 2020-05-26 14:53  弈邪  阅读(73)  评论(0)    收藏  举报