problem with ISBN checker

Reply

Join Date: Mar 2006
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

 
0
  #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:

  1. public class DigitExtractor
  2. {
  3. public DigitExtractor(String code)
  4. {
  5. isbn = code;
  6. }
  7. public int nextDigit()
  8. {
  9. // convert to int
  10. digit = isbn.charAt(count) - 48;
  11.  
  12. count += 1;
  13.  
  14. return digit;
  15. }
  16. public int getLength()
  17. {
  18. return isbn.length();
  19. }
  20. public String getISBN()
  21. {
  22. return isbn;
  23. }
  24. private String isbn;
  25. private int digit;
  26. private int count;
  27. }

  1. public class ISBNChecker
  2. {
  3. public static void main(String[] args)
  4. {
  5. DigitExtractor myDigit = new DigitExtractor("123456789X");
  6. int sum = 0; // holds the sum
  7. int rem = 0; // holds the remainder
  8. int checkSum = 0; // holds the checkSum value
  9.  
  10. if (myDigit.getLength() == 10)
  11. {
  12. // extract the next digit and add it to the sum
  13. for (int cnt = 0; cnt < myDigit.getLength() - 1; cnt++)
  14. {
  15. int temp = myDigit.nextDigit() * (myDigit.getLength() - cnt);
  16. sum += temp;
  17. System.out.println(temp);
  18. }
  19.  
  20. System.out.println("sum: " + sum);
  21.  
  22. // get the remainder of sum / 11
  23. rem = sum % 11;
  24. System.out.println("remainder: " + rem);
  25.  
  26. // subtract remainder from 11 to get checkSum
  27. checkSum = 11 - rem;
  28.  
  29. System.out.println("checksum: " + checkSum);
  30. }
  31. else if (myDigit.getLength() == 13)
  32. {
  33. // extract the next digit and add it to the sum
  34. for (int cnt = 0; cnt < myDigit.getLength() - 1; cnt++)
  35. {
  36. int temp = myDigit.nextDigit();
  37.  
  38. if ((cnt + 1) % 2 == 0)
  39. {
  40. sum += temp * 3;
  41. System.out.println(temp * 3);
  42. }
  43. else
  44. {
  45. sum += temp;
  46. System.out.println(temp);
  47. }
  48. }
  49.  
  50. System.out.println("sum: " + sum);
  51. // get the remainder of sum/10
  52. rem = sum % 10;
  53. System.out.println("rem: " + rem);
  54.  
  55. // subtract remainder from 10 to get checkSum
  56. checkSum = 10 - rem;
  57.  
  58. System.out.println("checksum: " + checkSum);
  59. }
  60. else
  61. System.out.println("Invalid ISBN");
  62.  
  63. // check the sum to see if it is valid
  64. if (checkSum < 10 && (myDigit.getISBN().charAt(myDigit.getLength() - 1) == (checkSum + 48)))
  65. System.out.println("Valid ISBN - num");
  66. else if (checkSum >= 10 && (myDigit.getISBN().charAt(myDigit.getLength() - 1) == 'X'))
  67. System.out.println("Valid ISBN - X");
  68. else
  69. System.out.println("Invalid ISBN");
  70. }
  71. }
Last edited by degamer106; Oct 10th, 2007 at 2:47 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
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

 
0
  #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 Quick reply to this message  
Join Date: May 2007
Posts: 4,346
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: 498
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: problem with ISBN checker

 
0
  #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 Quick reply to this message  
Join Date: Nov 2004
Posts: 6,145
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: problem with ISBN checker

 
0
  #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):
  1. public class Eleven {
  2. private static boolean elevenProof(String number) {
  3. if (number.length() != 10) {
  4. return false;
  5. }
  6. int total = 0;
  7. for (int i = 0; i < 9; i++) {
  8. char c = number.charAt(i);
  9. int digit = Character.digit(c, 10);
  10. total += (10 - i) * digit;
  11. }
  12. int checksum = Character.digit(number.charAt(9), 10);
  13. int modulo = total % 11;
  14. return modulo == 11 - checksum;
  15. }
  16.  
  17. public static void main(String[] args) {
  18. String isbn = args[0];
  19. System.out.println(isbn + " is " + (elevenProof(isbn) ? "valid" : "invalid"));
  20. }
  21. }
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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC