# I am having trouble writing code that solves this problem involving check sums.

• January 21st, 2013, 06:36 PM
uncoordinated
I am having trouble writing code that solves this problem involving check sums.
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);

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.

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

Code java:

```  int 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 */ ;```
My Poor Code

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
"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
Code java:

```  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
• January 21st, 2013, 06:44 PM
Norm
Re: I am having trouble writing code that solves this problem involving check sums.
[code=java]
[/code]
to get highlighting and preserve formatting.

Also please remove the extra blank lines. A few are sufficient.

Can you explain what the problem is? Post the program's output and add some comments saying what is wrong with the output and describe what the output should be.
• January 21st, 2013, 07:30 PM
Zaphod_b
Re: I am having trouble writing code that solves this problem involving check sums.
Quote:

Originally Posted by uncoordinated
...help...

Here's some pseudo-code for a way to implement (and debug) the beast:, building on the code that you already have:
Code :

```  int main(String [] args) {   // For initial debugging, why not use the ISBN from the worked example? String[] c = {"0","-","2","0","1","-","5","3","0","8","2","-","1"}; int sum = 0; boolean good;   // Declare other variables as needed.   // Create a loop to calculate sum // // In the middle of the loop you might want to print each term that is // added into the sum. Print the value of pos and the integer value // of the digit in that position. Then print the value of the sum. //   // // After the loop, use the value of the sum to calculate multipleOf11 //   // // Print out everything that the program has been working on up to this point. // System.out.println("sum = " + sum + ", multipleOf11 = " + multipleOf11);     // Calculate the correct check digit. // int value = multipleOf11 - sum;   // Print out everything that the program will use in final calculation // System.out.println("value = " + value + ", c[12] = " + c[12]);   // Compare calculated check digit with the value in the ISBN // IF (c[12] is equal to "X") THEN good = (value == /* Whatever it is supposed to be for 'X' */); ELSE good = (value == /* The integer value parsed from String c[12] */); END IF   // Print out results   } // End of main()```

Note that an 'X' can only appear as a check digit. There will never be an 'X' as any other part of the ISBN, so I don't know why the instructor seems to have something in the template to handle it???

Cheers!

Z