ISBN Number Java

ISBN is another special number in Java. ISBN stands for the International Standard Book Number that is carried by almost each every book. The ISBN is a ten-digit unique number. With the help of the ISBN, we can easily find any book. The ISBN number is a legal number when 1*Digit1 + 2*Digit2 + 3*Digit3 + 4*Digit4 + 5*Digit5 + 6*Digit6 + 7*Digit7 + 8*Digit8 + 9*Digit9 + 10*Digit10 is divisible by 11. The digits are taken from right to left. So, if the ten-digit number is 7426985414, Digit1 and Digit10 will be 4 and 7, respectively.

Let’s take two numbers and check whether the numbers are legal or not.

Example:

Number1: 8147852369

Sum = (1*9) + (2*6) + (3*3) + (4*2) + (5*5) + (6*8) + (7*7) + (8*4) + (9*1) + (10*8)

Sum = 9 + 12 + 9 + 8 + 25 + 48 + 49 + 32 + 9 + 80

Sum = 281

Now, we divide the sum with 11 and check whether the remainder is 0 or not. If the remainder is 0, the number is a legal ISBN.

rem = 281 % 11

rem = 6 != 0

Number 8147852369 is not a legal ISBN because the remainder is not equal to the 0.

Number2: 1259060977

Sum = (1*10) + (2*9) + (5*8) + (9*7) + (0*6) + (6*5) + (0*4) + (9*3) + (7*2) + (7*1)

Sum = 10 + 18 + 40 + 63 + 0 + 30 + 0 + 27 + 14 + 7

Sum = 209

Now, we divide the sum with 11 and check whether the remainder is 0 or not.

rem = 209 % 11

rem = 0

Number 1259060977 is a legal ISBN because the remainder is equal to 0.

Steps to implement the ISBN program

These are the following steps that we use to implement the program of ISBN in Java.

  • Take a ten-digit ISB number from the user.
  • Check whether the user entered a ten-digit number or not because if the number is not a ten-digit number, it can’t be an ISBN.
  • Find each digit’s sum by multiplying each digit from left to right by 1, 2, 3,…….,10.
  • Find the remainder of the sum with 11. If the remainder is equal to 0, the number is an ISBN else, not an ISBN.

Let’s implement the code to check whether the number is an ISBN or not by using the above steps:

ISBNNumberExample.java

  1. //import required classes and packages  
  2. import java.util.*;   
  3. import java.io.*;   
  4. import java.util.Scanner;  
  5.   
  6. //create ISBNNumberExample class to check whether the given number is a valid ISBN or not  
  7. class ISBNNumberExample {   
  8.     
  9.      static boolean checkISBNNumber(long number)  
  10.     {  
  11.             int sum = 0;  
  12.     int i, t, intNumber, dNumber;  
  13.     String strNumber;  
  14.           
  15.     strNumber = “”+number;  
  16.           
  17.     if (strNumber.length() != 10) {  
  18.                     return false;  
  19.             }  
  20.           
  21.             for (i = 0; i < strNumber.length(); i++) {  
  22.                     intNumber = Integer.parseInt(strNumber.substring(i, i+1));  
  23.                     dNumber = i + 1;  
  24.                     t = dNumber * intNumber;  
  25.                     sum = sum + t;  
  26.             }  
  27.   
  28.             // check whether the sum is divisible by 11 or not  
  29.         
  30.             if ((sum % 11) == 0) {  
  31.                     return true;  
  32.             }  
  33.           
  34.     return false;  
  35.           
  36.     }  
  37.     
  38.     // main() method start  
  39.     public static void main(String args[])   
  40.     {     
  41. long n1, n2;  
  42.           
  43.     try {  
  44.               
  45.         //create BufferedReader class object to get input from user  
  46.         InputStreamReader in = new InputStreamReader(System.in);  
  47.         BufferedReader br = new BufferedReader(in);  
  48.           
  49.         //show custom message  
  50.         System.out.println(“Enter first 10 digit ISBN number”);  
  51.               
  52.         //store user entered value into variable n1  
  53.         n1 = Long.parseLong(br.readLine());  
  54.               
  55.         //show custom message  
  56.         System.out.println(“Enter second 10 digit ISBN number”);  
  57.               
  58.         //store user entered value into variable n2  
  59.         n2 = Long.parseLong(br.readLine());  
  60.               
  61.         if (checkISBNNumber(n1))   
  62.             System.out.println(n1 + ” is a valid ISBN number”);   
  63.         else  
  64.             System.out.println(n1 + ” is not a valid ISBN number”);   
  65.         if (checkISBNNumber(n2))   
  66.             System.out.println(n2 + ” is a a valid ISBN number”);   
  67.         else  
  68.             System.out.println(n2 + ” is not a valid ISBN number”);  
  69.               
  70.         }catch(java.lang.Exception e) {  
  71.             System.out.println(“Error while reading the data.”);  
  72.         }   
  73.         }   
  74. }  

Output

ISBN Number Java

Leave a Comment