RSS Forums RSS

problem with ISBN checker

Please support our Java advertiser: Programming Forums
Reply
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

problem with ISBN checker

  #1  
Oct 10th, 2007
I'm trying to get this ISBN checker to work. I've used the formula from this site and also this site and it seems to work fine except for the ISBN of my Java book. When I compute my book's ISBN (0131496980), it is apparently invalid. I have tried the other codes on both of those sites as well as other books I have at home and there is no problem. Here is my code:

public class DigitExtractor
{
	public DigitExtractor(String code)
	{
		isbn = code;
	}
	public int nextDigit()
	{
		// convert to int
		digit = isbn.charAt(count) - 48;

		count += 1;

		return digit;
	}
	public int getLength()
	{
		return isbn.length();
	}
	public String getISBN()
	{
		return isbn;
	}
	private String isbn;
	private int digit;
	private int count;
}

public class ISBNChecker
{
	public static void main(String[] args)
	{
		DigitExtractor myDigit = new DigitExtractor("123456789X");
		int sum = 0;		// holds the sum
		int rem = 0;		// holds the remainder
		int checkSum = 0;	// holds the checkSum value

		if (myDigit.getLength() == 10)
		{
			// extract the next digit and add it to the sum
			for (int cnt = 0; cnt < myDigit.getLength() - 1; cnt++)
			{
				int temp = myDigit.nextDigit() * (myDigit.getLength() - cnt);
				sum += temp;
				System.out.println(temp);
			}

			System.out.println("sum: " + sum);

			// get the remainder of sum / 11
			rem = sum % 11;
			System.out.println("remainder: " + rem);

			// subtract remainder from 11 to get checkSum
			checkSum = 11 - rem;

			System.out.println("checksum: " + checkSum);
		}
		else if (myDigit.getLength() == 13)
		{
			// extract the next digit and add it to the sum
			for (int cnt = 0; cnt < myDigit.getLength() - 1; cnt++)
			{
				int temp = myDigit.nextDigit();

				if ((cnt + 1) % 2 == 0)
				{
					sum += temp * 3;
					System.out.println(temp * 3);
				}
				else
				{
					sum += temp;
					System.out.println(temp);
				}
			}

			System.out.println("sum: " + sum);
			// get the remainder of sum/10
			rem = sum % 10;
			System.out.println("rem: " + rem);

			// subtract remainder from 10 to get checkSum
			checkSum = 10 - rem;

			System.out.println("checksum: " + checkSum);
		}
		else
			System.out.println("Invalid ISBN");

		// check the sum to see if it is valid
		if (checkSum < 10 && (myDigit.getISBN().charAt(myDigit.getLength() - 1) == (checkSum + 48)))
			System.out.println("Valid ISBN - num");
		else if (checkSum >= 10 && (myDigit.getISBN().charAt(myDigit.getLength() - 1) == 'X'))
			System.out.println("Valid ISBN - X");
		else
			System.out.println("Invalid ISBN");
	}
}
Last edited by degamer106 : Oct 10th, 2007 at 1:47 am.
AddThis Social Bookmark Button
Reply With Quote  
Posts: 5
Reputation: kedarkhedkar is an unknown quantity at this point 
Solved Threads: 0
kedarkhedkar kedarkhedkar is offline Offline
Newbie Poster

Re: problem with ISBN checker

  #2  
Oct 10th, 2007
I also have a query about ISBN. Some books have one ISBN, SOME have two. Is it appropriate to use ISBN AS a primary key for a book table ( as I have seen it in one book).
What is ISBN. What can be the appropriate primary key for book & other products like laptops.
Reply With Quote  
Posts: 4,111
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 458
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Industrious Poster

Re: problem with ISBN checker

  #3  
Oct 10th, 2007
Originally Posted by kedarkhedkar View Post
I also have a query about ISBN. Some books have one ISBN, SOME have two. Is it appropriate to use ISBN AS a primary key for a book table ( as I have seen it in one book).
What is ISBN. What can be the appropriate primary key for book & other products like laptops.

Please post such questions as a new thread so the original question is not derailed.
Reply With Quote  
Posts: 5,749
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Solved Threads: 205
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: problem with ISBN checker

  #4  
Oct 10th, 2007
What you have here is proof that not everyone is going to play nice with "standards". Apparently Prentice Hall either miscalculated the ISBN for the book or simply don't care.

btw, your code is unnecessarilly long and convoluted.
The entire calculation can be handled in just a few lines of code (for ISBN10, similar code can be created for ISBN13):
public class Eleven {
	private static boolean elevenProof(String number) {
		if (number.length() != 10) {
			return false;
		}
		int total = 0;
		for (int i = 0; i < 9; i++) {
			char c = number.charAt(i);
			int digit = Character.digit(c, 10);
			total += (10 - i) * digit;
		}
		int checksum = Character.digit(number.charAt(9), 10);
		int modulo = total % 11;
		return modulo == 11 - checksum;
	}

	public static void main(String[] args) {
		String isbn = args[0];
		System.out.println(isbn + " is " + (elevenProof(isbn) ? "valid" : "invalid"));
	}
}
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Similar Threads
Other Threads in the Java Forum
Views: 1028 | Replies: 3 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:29 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC