Java
 

Home
Up
Time-clock, Attendance
Web Data Extractor
Polaris FTP
Polaris Stamp
Polaris Word Count
AS/400 Data Browser
PC File CR Inserter
PC File Merger
Order via PayPal
Purchase Order
Free Downloads
Support Request
Contact Us

Copyright © 2004
by Polaris Computing.
All rights reserved.

 

 

Java Code for ISBN Conversion

The following program converts a 10-character ISBN to a 13-digit ISBN with the "978" prefix, and vise versa. The function ISBN1013 can be used to convert your currently existing ISBN's into the new format.

You can use Microsoft Visual J++ to compile the following code into a console application.  Minor modifications might be needed if you use other compilers.  The main method simply passes an argument to one of the conversion functions, ISBN1310 and ISBN1013, and conversions are all done by these functions.

Language Java
Compiler Microsoft Visual J++
Disclaimer:  All sample code contained herein is provide to you  "AS IS."  ALL IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF THE MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE EXPRESSLY DISCLAIMED.
// This program gets an argument from the command line.
// If the argument is a 10-character SIBN, the program will
// print the corresponding 13-gigit ISBN.  If the argument
// is a 13-digit ISBN, the program will print the
// corresponding 10-character ISBN.
// The main program is only for testing. All the
// coversion is done by the two functions: ISBN1310
// and ISBN1013.  Both of these functions all another
// function, CharToInt, which returns the integer valude for
// a character if possible.
public class isbnconv {
  private static String CheckDigits = new String("0123456789X0");
  ///////////////////////////////// Main ////////////////////////////////
  public static void main(String[] args) {
    String ISBN = new String();
    if (args.length==1) {
      ISBN = args[0];
      if (ISBN.length()==10) System.out.println(ISBN1013(ISBN));
      else if (ISBN.length()==13) System.out.println(ISBN1310(ISBN));
      else ISBN = "Invalid ISBN";
    }
    else System.out.println("Usage: isbnconv ISBN");
    // The line above works for Microsoft Visual J++ while you test the
    // code in the project directory.  You may need to change it to the
    // following line.
    // else System.out.println("Usage: java isbnconv ISBN");
  }
  /////////////// Change a character to its integer value ///////////////
  static int CharToInt(char a) {
    switch (a) {
      case '0':   return 0;
      case '1':   return 1;  
      case '2':   return 2;
      case '3':   return 3;  
      case '4':   return 4;
      case '5':   return 5;
      case '6':   return 6;
      case '7':   return 7;
      case '8':   return 8;
      case '9':   return 9;
      default:    return -1;
    }
  }
  ////////////////////// Convert ISBN-13 to ISBN-10 /////////////////////
  static String ISBN1310(String ISBN) {
    String s9;
    int i, n, v;
    boolean ErrorOccurred;
    ErrorOccurred = false;
    s9 = ISBN.substring(3, 12);
    n = 0;
    for (i=0; i<9; i++) {
      if (!ErrorOccurred) {
        v = CharToInt(s9.charAt(i));
        if (v==-1) ErrorOccurred = true;
        else n = n + (10 - i) * v; 
      }
    }
    if (ErrorOccurred) return "ERROR";
    else {
      n = 11 - (n % 11);
      return s9 + CheckDigits.substring(n, n+1); 
    }
  }

  ////////////////////// Convert ISBN-10 to ISBN-13 /////////////////////
  static String ISBN1013(String ISBN) {
    String s12;
    int i, n, v;
    boolean ErrorOccurred;
    ErrorOccurred = false;
    s12 = "978" + ISBN.substring(0, 9);
    n = 0;
    for (i=0; i<12; i++) {
      if (!ErrorOccurred) {
        v = CharToInt(s12.charAt(i));
        if (v==-1) ErrorOccurred = true;
        else {
          if ((i % 2)==0) n = n + v;
          else n = n + 3*v;
        }
      }
    }
    if (ErrorOccurred) return "ERROR";
    else {
      n = n % 10;
      if (n!=0) n = 10 - n;
      return s12 + CheckDigits.substring(n, n+1);
    }
  }
}