ISBN check
Can sum1 help me cos im writing this code which will allow me to check if any ISBN I input via keyboard is correct.
Currently iv managed to get the code which allows me to input from the keyboard and im stuck i pretty clueless bout this.
Can sum1 pls help! Thanx in advance!
You want to check if a string is a valid ISBN number in Java? Are you sure you're not getting confused with JavaScript?
If you are indeed looking for java, and not javascript, you could use this (based on php code found at http://www.planet-source-code.com/vb...Id=20&lngWId=8 )
Code:
public class ISBN {
public static boolean isValid(String ISBN) {
String theISBN=ISBN.replaceAll("[^\\dX]", "");
if(theISBN.length()!=10) return false;
int checkDigit=11;
for(int i=2; i<=10; i++) {
checkDigit+=i*(((int)theISBN.charAt(10-i))-48);
}
checkDigit=(11-(checkDigit%11));
char check=(char)(checkDigit+48);
if(checkDigit==10) check='X';
else if(checkDigit==11) check='0';
if(theISBN.charAt(9)==check) return true;
return false;
}
}
Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.
Hmm i was told that u had to parse the string into an integer before using it the calculation of the checksum digit.
Anyone else got any ideas?
In a way, I am parsing the string into an integer; in ascii, the number characters 0-9 have character codes 48-57, so by using (int)charAt(i)-48, I obtain the integer value of that character. I've tried my code on some ISBNs found on amazon.com, and then on some random permutations of numbers. It seems to work ok (ie return true for real ones and false for fake ones)
Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks