Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Simple Error

Options
  • 03-05-2012 3:16pm
    #1
    Registered Users Posts: 8,324 ✭✭✭


    I'm probably just making a stupid math error, but I just can't see it. The code compiles and runs. It's basically a quiz question from the book I'm using. The point is to get the user to enter 9 digits for an ISBN code. The 10th digit is devised using digit 1 * 1 + digit 2 * 2 + digit 3 * 3 and so on and so forth with the total being modded by 11.

    For entering 1 2 3 4 5 6 7 8 9, I am getting 1 2 3 4 5 6 7 8 9 208 (where 208 should be 3)

    Bear in mind that I'm kinda jumping ahead of myself with arrays and that's based off stuff I did in college 5 years ago. The book hasn't hit do while or arrays yet, but I'm not to remember as best I can.
    import java.util.Scanner;
    
    
    public class ISBN {
    	
    	
    	public static void main(String args[])
    	{
    		
    		System.out.println("Enter ISBN with space between numbers (9 digits)");
    		Scanner input = new Scanner(System.in);
    		int[] ISBNinput;
    		ISBNinput = new int[10];
    		int counter = 0;
    		
    		do
    		{
    			ISBNinput[counter] = input.nextInt();
    			counter++;
    				
    		}while (counter <= 8);
    		
    		ISBNinput[9] = (((ISBNinput[0] * 1) + (ISBNinput[1] * 2) + (ISBNinput[2] * 3) + (ISBNinput[3] * 4) 
    				+ (ISBNinput[4] * 5) + (ISBNinput[5] * 6) + (ISBNinput[6] * 7) + (ISBNinput[7] * 8)
    				+ (ISBNinput[8] * 9)) % 11);
    		
    		for (counter = 0; counter <= 9;)
    		{
    			System.out.print(ISBNinput[counter]); counter++;
    		}
    		
    		}
    	}
    
    
    


Comments

  • Registered Users Posts: 10,624 ✭✭✭✭28064212


    That code's correctly returning 12345678910 for me:
    ((1 * 1) + (2 * 2) + (3 * 3) + (4 * 4) + (5 * 5) + (6 * 6) + (7 * 7) + (8 * 8) + (9 * 9)) % 11 = 10

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Registered Users Posts: 880 ✭✭✭moycullen14


    IIRC ISBN codes are supposed to sum to 0 mod 11 (it's a checksum/error correction thing)

    (10 * digit1 + 9 * digit2 + 8 * digit3 +....... 1 * digit10) mod 11 = 0

    Given the 9 digits, you need to work out the 10th (digit10)

    So say S = (10 * digit1 + 9 * digit2 + 8 * digit3 +.......2*digit9) mod 11

    then (S + digit10) mod 11 = 0
    S      digit10       (11 - S) mod 11
    0      0               0
    1      10              10
    2      9                9
    ...
    9      2
    10     1                1
    
    So,

    digit10 = (11 - S) mod 11

    I think thats it?


  • Registered Users Posts: 8,324 ✭✭✭chrislad


    IIRC ISBN codes are supposed to sum to 0 mod 11 (it's a checksum/error correction thing)

    (10 * digit1 + 9 * digit2 + 8 * digit3 +....... 1 * digit10) mod 11 = 0

    Given the 9 digits, you need to work out the 10th (digit10)

    So say S = (10 * digit1 + 9 * digit2 + 8 * digit3 +.......2*digit9) mod 11

    then (S + digit10) mod 11 = 0
    S      digit10       (11 - S) mod 11
    0      0               0
    1      10              10
    2      9                9
    ...
    9      2
    10     1                1
    
    So,

    digit10 = (11 - S) mod 11

    I think thats it?

    Cheers, that's it. I misread the spec slightly. Wasn't overly concerned as it is just practice and the code compiled and ran fine. Thanks again!


Advertisement