Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

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

  1. main reads the line into s with a Scanner and creates the object ob with new ISBN10(s). The constructor stores s in isbn.
  2. main calls ob.hasValidLength(), which returns true if isbn.length() is 10. If not, it prints INVALID ISBN - wrong length and stops.
  3. main calls ob.hasValidChars():
    • a loop checks ch = isbn.charAt(i) for i from 0 to 9;
    • a character from '0' to '9' is fine anywhere, and 'X' is fine only when i is 9;
    • any other character makes the method return false, and main prints INVALID ISBN - invalid character.
  4. main calls ob.isValid(), which returns checkSum() % 11 == 0. If it is false, main prints INVALID ISBN - checksum fails.
  5. checkSum() adds valueAt(i) * (10 - i) for i from 0 to 9, so the first character has weight 10 and the last has weight 1.
  6. valueAt(i) returns 10 for 'X', and otherwise (int)(ch - '0'), the value of the digit character.
  7. For a valid ISBN, main prints VALID ISBN and Weighted sum = followed by ob.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

VariableData TypePurpose
isbnStringData member that stores the string to be checked
sStringParameter 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
iintLoop counter or parameter: the position of the character (0 to 9)
chcharThe character of isbn at position i
scScannerReads the line typed by the user
obISBN10Object of the class created in main
argsString[]Command-line arguments of main (not used)

Sample input and output

Sample 1

Enter the ISBN: 0306406152
VALID ISBN
Weighted sum = 132

Sample 2

Enter the ISBN: 080442957X
VALID ISBN
Weighted sum = 209

Sample 3

Enter the ISBN: 0306406153
INVALID ISBN - checksum fails

Sample 4

Enter the ISBN: 12345
INVALID ISBN - wrong length

Sample 5

Enter the ISBN: 03064X6152
INVALID ISBN - invalid character

Sample 6

Enter the ISBN: 03064a6152
INVALID ISBN - invalid character

One value per line

Dry 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.

ichvalueAt(i)WeightProducts after the step
0001000
13392727
2008027
36674269
44462493
5005093
666424117
71133120
855210130
92212132

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

  1. 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."
  2. Check digit "Accept the first 9 digits of an ISBN-10 and find its check character."