I will provide the question an then what I tried to do with my code.
Info
International Standard Book Numbers
The final character of a ten-digit International Standard Book Number (ISBN) is a check digit that is computed as follows:
Working from right to left,
multiply each digit (other than the as-yet-unknown check digit) by its position in the number (including the as-yet-unknown check digit);
add the products together;
subtract the total from the closest multiple of 11 greater than or equal to that total. The answer is the check digit (using the letter "X" if the answer happens to be 10).
Consider, for example, the ISBN 0-201-53082-1.
Calculating the digit-by-position products yields
2 × 2 = 4; 8 × 3 = 24; 0 × 4 = 0; 3 × 5 = 15; 5 × 6 = 30;
1 × 7 = 7; 0 × 8 = 0; 2 × 9 = 18; 0 × 10 = 0.
Adding the products together yields
4 + 24 + 0 + 15 + 30 + 7 + 0 + 18 + 0 = 98.
Subtracting 98 from 99 yields the check digit 1.
Since our calculation yields the same result as the final digit of the given ISBN, we conclude that this ISBN is valid.
While this may seem more complicated than UPC check digits, it can be validated very simply by adding together all the digit-by-position products (including the check digit, whose position is of course 1). If the result is a multiple of 11, then the ISBN is valid. (Recall that to check if an int n is a multiple of 11, determine whether or not n % 11 is 0.)
Question
The String Array c contains the "stringified" individual elements (including hypens) of a ten-digit ISBN. (In the above example, the contents of c would be "0", "-", "2", "0", "1", "-", "5", "3", "0", "8", "2", "-", "1", in that order.) Complete the following code to store in the boolean variable good the value true if and only if the ISBN is valid.
Template Provided by the instructor
My Poor Codeint sum = 0; int pos = 1; // Loop over array from end to start for ( /* Your code here */ ) { // Ignore hyphens if ( ! /* Your code here */ .equals( "-" ) ) { // Deal with a possible "X" digit if ( /* Your code here */ ) sum += /* Your code here */ ; else sum += /* Your code here */ ; pos++; } } good = /* Your code here */ ;
WARNING VERY BAD AND INEFFICIENT
Ignore The Print Line I had I was trying to figure out where I was going wrong oh also the instruction provided sample test things
and the answers right here:
"1","-","2","3","4","-","5","6","7","8","9","-","X" Should come out as true
"0","-","2","0","1","-","5","3","0","8","2","-","1" good should be true again
"6","-","2","9","7","-","4","5","6","3","1","-","7" good should be false
"9","-","2","9","7","-","5","4","1","3","6","-","9" good should be false
String[] c = { "9","-","2","9","7","-","5","4","1","3","6","-","9" }; boolean good; int sum = 0; int pos = 2; // Loop over array from end to start for ( int i = c.length - 2; i >= 0; i-- ) { // Ignore hyphens if ( !(c[i].equals( "-" )) ) { // Deal with a possible "X" digit if ( !(c[i] == "X") ) sum += Integer.parseInt(c[i]) * pos; else pos++; } } System.out.println(sum); int multipleOf11 = 11 * ( sum / 11 ); int value = multipleOf11 - sum; if ( multipleOf11 != sum ) multipleOf11 += 11; if(!((c[12]) == "X")){ good = (sum + value) % 11 == Integer.parseInt(c[12]); } else if(c[12] == "X") good = (sum + value) % 11 == 0; else good = false; System.out.println(good);
Wow this is long hopefully one of you takes the time to help me out and if you do then Thank You!
Sorry for all the typos