943,833 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2568
  • Java RSS
Oct 10th, 2007
0

problem with ISBN checker

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  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. }

Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
Oct 10th, 2007
0

Re: problem with ISBN checker

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kedarkhedkar is offline Offline
5 posts
since Sep 2007
Oct 10th, 2007
0

Re: problem with ISBN checker

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Oct 10th, 2007
0

Re: problem with ISBN checker

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):
Java Syntax (Toggle Plain Text)
  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. }
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Want to know something
Next Thread in Java Forum Timeline: code to search





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC