| | |
problem with ISBN checker
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Solved Threads: 0
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)
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; }
Java Syntax (Toggle Plain Text)
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 2:47 am.
•
•
•
•
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.
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):
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)
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.
![]() |
Similar Threads
- This or that? (Posting Games)
- Another Dell Inspiron 1000 problem or 2... (Troubleshooting Dead Machines)
- iBook G4: Airport problem? o.O (Apple Hardware)
- Big Problem (Viruses, Spyware and other Nasties)
- Windows XP Proffesional - Explorer problem (Windows NT / 2000 / XP)
- Big Problem (Viruses, Spyware and other Nasties)
- about:blank problem (Viruses, Spyware and other Nasties)
- Install problem~ (Windows NT / 2000 / XP)
Other Threads in the Java Forum
- Previous Thread: Want to know something
- Next Thread: code to search
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component database desktop draw ebook eclipse encode equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions rotatetext scanner score screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream






