java检测isbn是否合法
/** * An ISBN Utility * @author Julius Schorzman * (C) 2006 -- Provided under GPL */ public class ISBNUtil { /** * Checks if the passed string is a valid ISBN number. * @param isbn The ISBN String * @return true if it is a valid isbn; false in all other cases. */ public static boolean isISBN(String isbn) { try { if (isbn.length() != 10) { return false; } int weight = 10; int rollingSum = 0; for ( int i = 0 ; i < 9 ; i++ ) { int isbnDigit = Character.digit(isbn.charAt(i), 10); rollingSum += isbnDigit * weight--; } int mod = rollingSum % 11; mod = 11 - mod; if ( mod == 11 ) mod = 0; char checkSum = isbn.charAt(9); if (Character.toLowerCase(checkSum) == 'x') { if ( mod == 10 ) return true; } else { if (Character.digit(checkSum, 10) == mod) return true; } return false; } catch (Exception e) { return false; } } public static void main(String[] args) { System.out.println("true: "+ISBNUtil.isISBN("031234161X")); System.out.println("true: "+ISBNUtil.isISBN("0525949488")); System.out.println("true: "+ISBNUtil.isISBN("076360013X")); System.out.println("true: "+ISBNUtil.isISBN("0671027360")); System.out.println("true: "+ISBNUtil.isISBN("0803612079")); System.out.println("true: "+ISBNUtil.isISBN("0307263118")); System.out.println("true: "+ISBNUtil.isISBN("0684856093")); System.out.println("true: "+ISBNUtil.isISBN("0767916565")); System.out.println("true: "+ISBNUtil.isISBN("0071392319")); System.out.println("true: "+ISBNUtil.isISBN("1400032806")); System.out.println("true: "+ISBNUtil.isISBN("0765305240")); System.out.println("false: "+ISBNUtil.isISBN("0312341613")); System.out.println("false: "+ISBNUtil.isISBN("052594948X")); System.out.println("false: "+ISBNUtil.isISBN("0763600138")); System.out.println("false: "+ISBNUtil.isISBN("0671027364")); System.out.println("false: "+ISBNUtil.isISBN("080361207X")); System.out.println("false: "+ISBNUtil.isISBN("0307263110")); System.out.println("false: "+ISBNUtil.isISBN("0684856092")); System.out.println("false: "+ISBNUtil.isISBN("0767916567")); System.out.println("false: "+ISBNUtil.isISBN("0071392318")); System.out.println("false: "+ISBNUtil.isISBN("1400032801")); System.out.println("false: "+ISBNUtil.isISBN("0765305241")); } }