Java detects whether ISBN is legal

From , 3 Years ago, written in Java, viewed 213 times.
URL https://pastebin.vip/view/fd272fe0
  1. /**
  2.  * An ISBN Utility
  3.  * @author Julius Schorzman
  4.  * (C) 2006 -- Provided under GPL
  5.  */  
  6. public class ISBNUtil {  
  7.     /**
  8.      * Checks if the passed string is a valid ISBN number.
  9.      * @param isbn The ISBN String
  10.      * @return true if it is a valid isbn; false in all other cases.
  11.      */  
  12.     public static boolean isISBN(String isbn) {  
  13.          
  14.         try {  
  15.             if (isbn.length() != 10) {  
  16.                 return false;  
  17.             }  
  18.              
  19.             int weight = 10;  
  20.             int rollingSum = 0;  
  21.             for ( int i = 0 ; i < 9 ; i++ ) {  
  22.                 int isbnDigit = Character.digit(isbn.charAt(i), 10);  
  23.                 rollingSum += isbnDigit * weight--;  
  24.             }  
  25.              
  26.             int mod = rollingSum % 11;  
  27.             mod = 11 - mod;  
  28.             if ( mod == 11 ) mod = 0;  
  29.              
  30.             char checkSum = isbn.charAt(9);  
  31.             if (Character.toLowerCase(checkSum) == 'x') {  
  32.                 if ( mod == 10 ) return true;  
  33.             } else {  
  34.                 if (Character.digit(checkSum, 10) == mod) return true;  
  35.             }  
  36.             return false;  
  37.         } catch (Exception e) {  
  38.             return false;  
  39.         }  
  40.     }  
  41.      
  42.     public static void main(String[] args) {  
  43.         System.out.println("true: "+ISBNUtil.isISBN("031234161X"));  
  44.         System.out.println("true: "+ISBNUtil.isISBN("0525949488"));  
  45.         System.out.println("true: "+ISBNUtil.isISBN("076360013X"));  
  46.         System.out.println("true: "+ISBNUtil.isISBN("0671027360"));  
  47.         System.out.println("true: "+ISBNUtil.isISBN("0803612079"));  
  48.         System.out.println("true: "+ISBNUtil.isISBN("0307263118"));  
  49.         System.out.println("true: "+ISBNUtil.isISBN("0684856093"));  
  50.         System.out.println("true: "+ISBNUtil.isISBN("0767916565"));  
  51.         System.out.println("true: "+ISBNUtil.isISBN("0071392319"));  
  52.         System.out.println("true: "+ISBNUtil.isISBN("1400032806"));  
  53.         System.out.println("true: "+ISBNUtil.isISBN("0765305240"));  
  54.  
  55.         System.out.println("false: "+ISBNUtil.isISBN("0312341613"));  
  56.         System.out.println("false: "+ISBNUtil.isISBN("052594948X"));  
  57.         System.out.println("false: "+ISBNUtil.isISBN("0763600138"));  
  58.         System.out.println("false: "+ISBNUtil.isISBN("0671027364"));  
  59.         System.out.println("false: "+ISBNUtil.isISBN("080361207X"));  
  60.         System.out.println("false: "+ISBNUtil.isISBN("0307263110"));  
  61.         System.out.println("false: "+ISBNUtil.isISBN("0684856092"));  
  62.         System.out.println("false: "+ISBNUtil.isISBN("0767916567"));  
  63.         System.out.println("false: "+ISBNUtil.isISBN("0071392318"));  
  64.         System.out.println("false: "+ISBNUtil.isISBN("1400032801"));  
  65.         System.out.println("false: "+ISBNUtil.isISBN("0765305241"));  
  66.     }  
  67. }  
  68. //java/8307

Reply to "Java detects whether ISBN is legal"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website