How do you know if an ISBN is valid




ISBN stands for International Standard Book Number which uniquely identifies a publication using a 10 digit number.

When transcribing ISBN's, it is easy to introduce an error in doing so. The most likely errors are transposition (interchanging two adjacent digits) and single digit errors (a 7 for a 1).

By using a check digit, it is possible to eliminate single occurrences of these errors. Here's how.

The actual ISBN consists of 9 digits, plus the 10th digit which serves as a checksum. The weight sum of the digits must be a multiple of 11, which implies that at times the last digit must have a value of 10, in which case the letter X will be used.

The weights for the digits vary from 1 to 10 successively from the rightmost digit to the left.

The choice of 11 for the modulo calculation is important. It is a prime greater than 10, so the replacement of any single digit by any other one will always trigger an error in the checksum. Similarly, the transposition of adjacent digits will always trigger an error because the difference created will not exceed 10.

The following is a Java Applet that will request entry of an ISBN and perform the checking according to the above procedure.

More information may be obtained here


Execute the ISBN Checker


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class ISBNchecker extends JApplet implements ActionListener
{
	// GUI stuff
	JLabel lISBN=new JLabel("Enter an ISBN number:");
	JTextField tISBN=new JTextField(30);
	JButton bCheck=new JButton("Check");
	Container cPane=getContentPane();
	public void init()
	{
		cPane.setLayout(new FlowLayout());
		cPane.add(lISBN);
		cPane.add(tISBN);
		bCheck.addActionListener(this);
		cPane.add(bCheck);
		showStatus("Enter an ISBN number");
		cPane.setVisible(true);
		repaint();
	}

	private boolean Validate(String sISBN)
	{
		int sum=0;
		boolean returnValue=false;
		if(sISBN.substring(9,10).matches("[Xx]"))
		{	// case where ISBN ends with an X
			sISBN=sISBN.substring(0,9)+"0";
			sum=10;
		}
		for(int i=0; i<10; i++)
		{
			sum+=(10-i)*(Integer.parseInt(sISBN.substring(i,i+1)));
		}
		if(sum%11==0)returnValue=true;
		return returnValue;
	}

	public void actionPerformed(ActionEvent e)
	{
		String sMatch="[0-9][0-9][0-9][0-9][0-9]"
		+"[0-9][0-9][0-9][0-9][0-9Xx]";
		String sISBN=tISBN.getText();
		if(sISBN.matches(sMatch))
		{
			if(Validate(sISBN))
			{
				showStatus("The entered ISBN is valid!");
			}
			else
			{
				JOptionPane.showMessageDialog(null,
				"Sorry, the ISBN you have entered is"
				+ " not valid, please check your data!");
				showStatus("The entered ISBN is NOT valid!");
			}
		}
		else
		{
			JOptionPane.showMessageDialog(null,"Sorry, an ISBN should contain exactly 10 decimal digits, the last of which may be an X");
			tISBN.setText("");
			showStatus("Please enter another ISBN");
		}
	}

}