View Single Post
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