Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

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

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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);

    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

     
    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
    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I am having trouble writing code that solves this problem involving check sums.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: I am having trouble writing code that solves this problem involving check sums.

    Quote Originally Posted by uncoordinated View Post
    ...help...
    Here's some pseudo-code for a way to implement (and debug) the beast:, building on the code that you already have:
     
        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

Similar Threads

  1. [SOLVED] Would Really Appreciate Some Help!! Trouble Writing XML File From GUI :(
    By Raggazz4 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 11th, 2012, 07:37 AM
  2. Having trouble writing the methods for this program
    By michael305rodri in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 1st, 2012, 11:31 PM
  3. Having trouble writing insert method for Ordered LinkList.
    By orbin in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 28th, 2012, 05:25 PM
  4. Trouble with writing/reading array to/from file
    By MaximusPrime in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2012, 08:41 PM
  5. Trouble writing some code...help?
    By bChEos in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 7th, 2010, 08:54 PM