# ISBN Number in Java

ISBN understands for International Standard Book Number, which is used to identify a book. In this post, we will write a Java program to check whether the given number is a valid ISBN number or not?

From 1970 to the end of December 2006, It is a 10 digit number, but since 1 January 2007 they now always consist of 13 digits.

The ISBN number having 10 digits is called ISBN-10 similarly, the ISBN number having 13 digits is called ISBN-13. Nowadays ISBN-10 is outdated and converted to ISBN-13.

## ISBN-10

The first nine digits of the ISBN number are used to represent the Title, Publisher and Group of the book and the last digit is used for checking whether the ISBN is correct or not.

For ISBN the sum of all ten digits, each multiplied by its weight in ascending order from 1 to 10, is a multiple of 11. The ISBN-10 can be represent as,

x1 + 2x2 + 3x3 + 4x4 + 5x5 + 6x6 + 7x7 + 8x8 + 9x9 + 10x10 = k*11, where k is an integer.

Example:-
Number = 0306406152
Sum = (0*1) + (3*2) + (0*3) + (6*4) + (4*5) + (0*6) + (6*7) + (1*8) + (5*9) + (2*10)
=> Sum = 0 + 6 + 0 + 24 + 20 + 0 + 42 + 8 + 45 + 20
=> Sum = 165, and 165 / 11 = 15
Since 165 is divisible by 11 so 0306406152 is a valid ISBN-10.

The first 9 digits of the ISBN-10 number can have any value between 0 to 9, but the last digit can have values between 0 to 10, and the 10 is represented as X.

Another eaxmple of ISBN:-
Number = 007462542X
Sum = (0*1) + (0*2) + (7*3) + (4*4) + (6*5) + (2*6) + (5*7) + (4*8) + (2*9) + (10*10)
=> Sum = 0 + 0 + 21 + 16 + 30 + 12 + 35 + 32 + 18 + 100
=> Sum = 264
And 264 is divisible of 11 as 24*11 = 264, so 007462542X is a valid ISBN-10.

## ISBN-13

In ISBN-13 the sum of all the thirteen digits, each multiplied by its (integer) weight, alternating between 1 and 3, is a multiple of 10.

x1 + 3x2 + x3 + 3x4 + x5 + 3x6 + x7 + 3x8 + x9 + 3x10 + x11 + 3x12 + x13 = k*10, where k is an integer.

Example:-
Number = 9780306406157
Sum = ( 9*1) + (7*3) + (8*1) + (0*3) + (3*1) + (0*3) + (6*1) + (4*3) + (0*1) + (6*3) + (1*1) + (5*3) + (7*1)
=> Sum = 9 + 21 + 8 + 0 + 3 + 0 + 6 + 12 + 0 + 18 + 1 + 15 + 7
=> Sum = 100, it ends with 0 so it is divisible by 10.
Hence 9780306406157 is an ISBN-13.

The last digit of ISBN-13 must range from 0 to 9.

The difference between an ISBN-13 and ISBN-10 is the prefix “978” at the front of the 13 digit, which results in a different check digit (the last number, or letter x). An ISBN-10 is converted to ISBN-13 by prepending “978” to the ISBN-10 and recalculating the final checksum digit using the ISBN-13 algorithm.

## Procedure to develop Java method to check number is ISBN or not

Note:- ISBN number may contain space or – like 978-3-16-148410-0

1) Take a number as String
2) Remove hypen or space
3) Check is it a number or not
4) Find its length, if length is 10 then check for ISBN-10 or if the length is 13 then check for ISBN-13 else it is not a isbn number.

Procedure to Check for ISBN-10

1) Take a number as String
2) Up to 9th digit, fetch every character, convert it into number and then calculate the sum
3) For the last digit check it 10 or not because 10 will be represented as x or X, not as a normal number, and then calculate the sum based on that number.
4) Now, check the sum is completely divisible by 11 or not? If yes then it is ISBN-10

Procedure to Check for ISBN-13

1) Take a number as String
2) Fetch every character and for digits on odd place multiply with 1 and digits on even place multiply with 3, then calculate the sum value
3) Check the sum is completely divisible by 10 or not? If yes then it is ISBN-13

## Java program for ISBN number

``````import java.util.Scanner;

public class Test1 {

// method to check number is ISBN
public static boolean isISBN(String number) {

// declare variable
int length = 0;

// remove all hyphens
number = number.replace("-", "");
// remove all spaces
number = number.replace(" ", "");

// check result string is a number or not
try {
// except for the case where
// ISBN-10 ends with X or x
char ch = number.charAt(9);
ch = Character.toUpperCase(ch);
if( ch != 'X') {
// don't store, only check
Long.parseLong(number);
}
} catch(NumberFormatException nfe) {
// not a number
return false;
}

// find length
length = number.length();
if(length==13)
return isISBN13(number);
else if(length==10)
return isISBN10(number);

return false;
}

// method to check ISBN-10
private static boolean isISBN10(String number) {

// declare variables
int sum = 0;
int digit = 0;
char ch = '\0';

for(int i=1; i<=9; i++) {
ch = number.charAt(i-1);
digit = Character.getNumericValue(ch);
sum += (i* digit);
}

// last digit
ch = number.charAt(9);
ch = Character.toUpperCase(ch);
if(ch =='X')
sum += (10*10);
else {
digit = Character.getNumericValue(ch);
sum += (digit * 10);
}

// check sum
if(sum % 11 == 0)
return true;

return false;
}

// method to check ISBN-13
private static boolean isISBN13(String number) {

// declare variables
int sum = 0;
int multiple = 0;
char ch = '\0';
int digit = 0;

for(int i=1; i<=13; i++) {

if(i % 2 == 0)
multiple = 3;
else multiple = 1;

// fetch digit
ch = number.charAt(i-1);
// convert it to number
digit = Character.getNumericValue(ch);

sum += (multiple*digit);
}

// check sum
if(sum%10 == 0)
return true;
return false;
}

// main method
public static void main(String[] args) {

// declare variables
String number = null;
boolean result = false;

//create Scanner class object to take input
Scanner scan = new Scanner(System.in);

// take input from end-user
System.out.print("Enter number::");
number = scan.nextLine();

// check number is isbn number or not
result = isISBN(number);

// display result
if(result)
System.out.println(number +
" is an isbn number.");
else
System.out.println(number +
" is not an isbn number.");

// close Scanner class object
scan.close();
}
}``````

The output for the different test-cases are:-

Enter number:: 978-3-16-148410-0
978-3-16-148410-0 is an isbn number.

Enter number:: 007462542X
007462542X is an isbn number.

Enter number:: 978-3-1X-1X8X10-0
978-3-1X-1X8X10-0 is not an isbn number.

In most books, the ISBN number can be found on the back cover, next to the barcode. If a book doesn’t show the ISBN on the back cover, look on the page featuring the copyright and publisher information and the ISBN will be found there.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!