Another question, this time regarding breaking a string down for validity
Thanks a bunch for the tip on the static to all of you folks who answered! Feeling a little less frustrated now.
I am not going to ask questions step by step through my whole assignment, but I want to make sure that this is the way to go about one of the next tasks. I have written the following, which compiles fine (the purpose is to check the string to make sure that it is numeric, and the user may have also entered the ISBN as a number with or without dashes):
private String validateISBN(String bookNum)
{
String[] book;
int j=0;
for ( int i=0;i<bookNum.length();i++)
if (character.isDigit(bookNum.charAt[i]))
bookNum.charAt[i]=book[j];j++;
I haven't written the next part, which has to allow for an X as the last digit in the string (which is apparently how ISBN numbers work). I would assume that if the above is correct (or close), that all i need to do is check that the ninth character is a digit or an X, by wri开发者_运维问答ting something like: if book[9] isDigit() || if book[9] == "x" || if book[9] == "X";
Is that about right (ISBN numbers are always 10 numbers or 9 numbers and an X at the end)?
The last digit of ISBN-10 is the check digit. Since your method is supposed to check if the entered ISBN is correct, you have to calculate the check digit on your own and compare it to the given one (plus making sure all characters are digits).
If you're not knowing what this means, read the validating ISBN-10 section at http://en.wikipedia.org/wiki/International_Standard_Book_Number#Check_digits
The 2001 edition of the official manual of the International ISBN Agency says that the ISBN-10 check digit[18] — which is the last digit of the ten-digit ISBN — must range from 0 to 10 (the symbol X is used instead of 10) and must be such that the sum of all the ten digits, each multiplied by the integer weight, descending from 10 to 1, is a multiple of the number 11. Modular arithmetic is convenient for calculating the check digit using modulus 11. Each of the first nine digits of the ten-digit ISBN — excluding the check digit, itself — is multiplied by a number in a sequence from 10 to 2, and the remainder of the sum, with respect to 11, is computed. The resulting remainder, plus the check digit, must equal 11; therefore, the check digit is 11 minus the remainder of the sum of the products.
On a related note: there is also the 13-digit ISBN-13...
Maybe your solution might look like this:
Remove the dashes, break down the string
to an int
array, compute the tenth-digit (see wiki-link), if all is fine return the input string.
精彩评论