ISC Practicals and Theory - ISBN-10 Validator (OOP)
Updated 21 Oct 2026
An ISBN-10 is the old 10-character code that identifies a book. Its last character is a check character, so a mistyped code can be detected by a simple calculation. This is the OOP version: main reads the string and passes it to a parameterised constructor, and the methods hasValidLength(), hasValidChars(), valueAt(), checkSum() and isValid() each do one job and return a value; only main prints.
The program prints VALID ISBN with the weighted sum, or INVALID ISBN with the reason: wrong length, invalid character or checksum fails. Only a capital X is accepted as the check character, and only in the last position; a small x is rejected as an invalid character.
Definition
A string is a valid ISBN-10 when:
- it has exactly 10 characters;
- the first 9 are digits and the 10th is a digit or the letter X, which stands for the value 10;
- the weighted sum $10 d_1 + 9 d_2 + 8 d_3 + \ldots + 2 d_9 + 1 d_{10}$ is divisible by 11.
For example, $0306406152$ gives $0 \times 10 + 3 \times 9 + 0 \times 8 + 6 \times 7 + 4 \times 6 + 0 \times 5 + 6 \times 4 + 1 \times 3 + 5 \times 2 + 2 \times 1 = 132$, and $132 = 11 \times 12$, so it is valid. Changing the last digit to 3 gives 133, so $0306406153$ is invalid.
Algorithm
mainreads the line intoswith aScannerand creates the objectobwithnew ISBN10(s). The constructor storessinisbn.maincallsob.hasValidLength(), which returnstrueifisbn.length()is 10. If not, it printsINVALID ISBN - wrong lengthand stops.maincallsob.hasValidChars():- a loop checks
ch = isbn.charAt(i)forifrom 0 to 9; - a character from
'0'to'9'is fine anywhere, and'X'is fine only wheniis 9; - any other character makes the method return
false, andmainprintsINVALID ISBN - invalid character.
- a loop checks
maincallsob.isValid(), which returnscheckSum() % 11 == 0. If it isfalse,mainprintsINVALID ISBN - checksum fails.checkSum()addsvalueAt(i) * (10 - i)forifrom 0 to 9, so the first character has weight 10 and the last has weight 1.valueAt(i)returns 10 for'X', and otherwise(int)(ch - '0'), the value of the digit character.- For a valid ISBN,
mainprintsVALID ISBNandWeighted sum =followed byob.checkSum().
Exam tip: (int)(ch - '0') turns a digit character into its value. Use it instead of Integer.parseInt() or Character.isDigit() when the question asks you to work on each character.
Program
import java.util.Scanner;
/* Class ISBN10: checks whether a string is a valid ISBN-10.
A valid ISBN-10 has exactly 10 characters: 9 digits, then a check character that is
a digit or the capital letter X (value 10). The weighted sum
d1 x 10 + d2 x 9 + ... + d10 x 1 must be divisible by 11.
Every method does one job and returns its answer; main prints the messages. */
public class ISBN10
{
String isbn; // the string to be checked
// Parameterised constructor
// s : the string typed by the user
ISBN10(String s)
{
isbn = s;
}
// Returns true if the string has exactly 10 characters
boolean hasValidLength()
{
return isbn.length() == 10;
}
// Returns true if the first 9 characters are digits and the last is a digit or capital X.
// Call it only when the length is 10.
boolean hasValidChars()
{
for (int i = 0; i < 10; i++)
{
char ch = isbn.charAt(i); // character at position i
if (ch >= '0' && ch <= '9')
continue; // a digit is fine at any position
if (ch == 'X' && i == 9)
continue; // capital X is fine only at the last position
return false; // anything else is invalid
}
return true;
}
// Returns the numeric value of the character at position i: 0 to 9 for a digit, 10 for X.
// i : position of the character (0 to 9). Call it only for a valid character.
int valueAt(int i)
{
char ch = isbn.charAt(i);
if (ch == 'X')
return 10;
return (int)(ch - '0'); // digit character to its value
}
// Returns the weighted sum: first value x 10 + second x 9 + ... + tenth x 1
int checkSum()
{
int s = 0; // running weighted sum
for (int i = 0; i < 10; i++)
s = s + valueAt(i) * (10 - i); // weight is 10 for the first character down to 1
return s;
}
// Returns true if the weighted sum is divisible by 11
boolean isValid()
{
return checkSum() % 11 == 0;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in); // reads the string
System.out.print("Enter the ISBN: ");
String s = sc.nextLine(); // string typed by the user
ISBN10 ob = new ISBN10(s); // object of the class
if (!ob.hasValidLength())
System.out.println("INVALID ISBN - wrong length");
else if (!ob.hasValidChars())
System.out.println("INVALID ISBN - invalid character");
else if (!ob.isValid())
System.out.println("INVALID ISBN - checksum fails");
else
{
System.out.println("VALID ISBN");
System.out.println("Weighted sum = " + ob.checkSum());
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
isbn | String | Data member that stores the string to be checked |
s | String | Parameter of the constructor, and in main the string typed by the user; in checkSum() the name s is an int holding the running weighted sum |
i | int | Loop counter or parameter: the position of the character (0 to 9) |
ch | char | The character of isbn at position i |
sc | Scanner | Reads the line typed by the user |
ob | ISBN10 | Object of the class created in main |
args | String[] | Command-line arguments of main (not used) |
Sample input and output
Sample 1
Enter the ISBN: 0306406152
VALID ISBN
Weighted sum = 132Sample 2
Enter the ISBN: 080442957X
VALID ISBN
Weighted sum = 209Sample 3
Enter the ISBN: 0306406153
INVALID ISBN - checksum failsSample 4
Enter the ISBN: 12345
INVALID ISBN - wrong lengthSample 5
Enter the ISBN: 03064X6152
INVALID ISBN - invalid characterSample 6
Enter the ISBN: 03064a6152
INVALID ISBN - invalid characterDry run
Take the input $0306406152$. The length is 10 and every character is valid, so checkSum() runs. The value valueAt(i) is multiplied by the weight 10 - i.
| i | ch | valueAt(i) | Weight | Product | s after the step |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 10 | 0 | 0 |
| 1 | 3 | 3 | 9 | 27 | 27 |
| 2 | 0 | 0 | 8 | 0 | 27 |
| 3 | 6 | 6 | 7 | 42 | 69 |
| 4 | 4 | 4 | 6 | 24 | 93 |
| 5 | 0 | 0 | 5 | 0 | 93 |
| 6 | 6 | 6 | 4 | 24 | 117 |
| 7 | 1 | 1 | 3 | 3 | 120 |
| 8 | 5 | 5 | 2 | 10 | 130 |
| 9 | 2 | 2 | 1 | 2 | 132 |
checkSum() returns 132 and $132 \% 11 = 0$, so isValid() returns true and main prints VALID ISBN and Weighted sum = 132, the same as the first sample run.
Variations
- ISBN-13 "Write a program to check whether a 13-digit ISBN is valid, using the weights 1 and 3 alternately and a sum divisible by 10."
- Check digit "Accept the first 9 digits of an ISBN-10 and find its check character."