ISBN Number in Java

In Java, the ISBN (International Standard Book Number) is a special number used for book identification. It consists of ten digits and allows for efficient book retrieval. To determine the validity of an ISBN, a specific calculation is performed.

The calculation involves multiplying each digit of the ISBN by a weight factor, starting from 1 for the rightmost digit and incrementing by 1 for each subsequent digit. The resulting products are then summed. For example, in ISBN 7426985414, the rightmost digit is Digit1 (4), and the leftmost digit is Digit10 (7).

To check if the ISBN is valid, the sum of the products 1*Digit1 + 2*Digit2 + 3*Digit3 + 4*Digit4 + 5*Digit5 + 6*Digit6 + 7*Digit7 + 8*Digit8 + 9*Digit9 + 10*Digit10 must be divisible by 11. If the sum is divisible by 11, the ISBN is considered legal.

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 have to divide the sum by 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 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 by 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

Here are the steps involved in implementing an ISBN program in Java:

  1. Prompt the user to enter a ten-digit ISBN number.
  2. Verify if the input consists of exactly ten digits since an ISBN number must be ten digits long. If the number does not have ten digits, it cannot be a valid ISBN.
  3. Calculate the sum of each digit by multiplying them from left to right by the corresponding weights 1, 2, 3, …, 10.
  4. Take the sum modulo 11 to find the remainder. If the remainder is 0, then the number is considered a valid ISBN. Otherwise, it is not a valid ISBN.

ISBNNumberExample.java

//import required classes and packages  
import java.util.*;   
import java.io.*;   
import java.util.Scanner;  
  
//create ISBNNumberExample class to check whether the given number is a valid ISBN or not  
class ISBNNumberExample {   
    
     static boolean checkISBNNumber(long number)  
    {  
            int sum = 0;  
    int i, t, intNumber, dNumber;  
    String strNumber;  
          
    strNumber = ""+number;  
          
    if (strNumber.length() != 10) {  
                    return false;  
            }  
          
            for (i = 0; i < strNumber.length(); i++) {  
                    intNumber = Integer.parseInt(strNumber.substring(i, i+1));  
                    dNumber = i + 1;  
                    t = dNumber * intNumber;  
                    sum = sum + t;  
            }  
  
            // check whether the sum is divisible by 11 or not  
        
            if ((sum % 11) == 0) {  
                    return true;  
            }  
          
    return false;  
          
    }  
    
    // main() method start  
    public static void main(String args[])   
    {     
long n1, n2;  
          
    try {  
              
        //create BufferedReader class object to get input from user  
        InputStreamReader in = new InputStreamReader(System.in);  
        BufferedReader br = new BufferedReader(in);  
          
        //show custom message  
        System.out.println("Enter first 10 digit ISBN number");  
              
        //store user entered value into variable n1  
        n1 = Long.parseLong(br.readLine());  
              
        //show custom message  
        System.out.println("Enter second 10 digit ISBN number");  
              
        //store user entered value into variable n2  
        n2 = Long.parseLong(br.readLine());  
              
        if (checkISBNNumber(n1))   
            System.out.println(n1 + " is a valid ISBN number");   
        else  
            System.out.println(n1 + " is not a valid ISBN number");   
        if (checkISBNNumber(n2))   
            System.out.println(n2 + " is a a valid ISBN number");   
        else  
            System.out.println(n2 + " is not a valid ISBN number");  
              
        }catch(java.lang.Exception e) {  
            System.out.println("Error while reading the data.");  
        }   
        }   
}

Output


By regularly following tutorials.freshersnow.com, you can learn more about implementing ISBN Number in Java.