# [Homework] ISBN Check digit source code help

Page 1 of 1

## 3 Replies - 19908 Views - Last Post: 14 February 2008 - 06:40 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=43239&amp;s=bc6b7b15ab5787e3cb1460c6feb07add&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lpporres1

Reputation: 0
• Posts: 2
• Joined: 13-February 08

# [Homework] ISBN Check digit source code help

Posted 13 February 2008 - 06:58 PM

I've started an Object Oriented Java Programming class but my code is still horribly lame.
I have to develop a program that runs in the console that asks for a 10 digit ISBN number and checks if its valid and corrects its last digit (the check digit) if its not valid, kinda like the applet in this page: http://www.cs.queens...t/isbncheck.htm
(the page is a reference site given by the teacher but it only gives me an idea of what the program has to display)
Anyway, i've managed to get a code that should take 9 digits and generate the check digit by computing the necessary calculations, here's the code:
```public class ISBN
{

public static void main(String[] args)
{

int N = new Integer.parseInt(args[0]);

int sum = 0;
for (int i = 2; i <= 10; i++)
{
int digit = N % 10;				// rightmost digit
sum = sum + i * digit;
N = N / 10;
}

System.out.print("The full ISBN number is " + args[0]);
if	  (sum % 11 == 1) System.out.println("X");
else if (sum % 11 == 0) System.out.println("0");
else					System.out.println(11 - (sum % 11));
}
}

```

It compiles correctly but when i try to run it, it gives me this:
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at ISBN.main(ISBN.java:10) "

Also i would like if someone could give me ideas on how to ask for a 10 digit ISBN and check if the check digit is correct,
THANK YOU SO MUCH FOR YOUR HELP

Is This A Good Question/Topic? 0

## Replies To: [Homework] ISBN Check digit source code help

### #2 PennyBoki

• D.I.C Lover

Reputation: 55
• Posts: 2,345
• Joined: 11-December 06

## Re: [Homework] ISBN Check digit source code help

Posted 13 February 2008 - 09:52 PM

Ok see this code:

```import java.lang.Integer;
public class ISBN
{

public static void main(String[] args)
{

Integer N = new Integer(5354);

int sum = 0;
for (int i = 2; i <= 10; i++)
{
int digit = N % 10;				// rightmost digit
sum = sum + i * digit;
N = N / 10;
}

System.out.print("The full ISBN number is " + 5354);
if	  (sum % 11 == 1) System.out.println("X");
else if (sum % 11 == 0) System.out.println("0");
else					System.out.println(11 - (sum % 11));
}
}
```

I haven't used args[0]. So there is where the exception comes from. So if you want to take arguments write this in the command line:

java ISBN 5354

see what happens, and use your code not mine. Cheers.

### #3 bhandari

Reputation: 9
• Posts: 754
• Joined: 31-January 08

## Re: [Homework] ISBN Check digit source code help

Posted 14 February 2008 - 06:37 AM

Quote

for (int i = 2; i <= 10; i++)
{
int digit = N % 10; // rightmost digit
sum = sum + i * digit;
N = N / 10;
}

lpporres1 Are you sure you will get what you want, irrespective of the input?

### #4 lpporres1

Reputation: 0
• Posts: 2
• Joined: 13-February 08

## Re: [Homework] ISBN Check digit source code help

Posted 14 February 2008 - 06:40 PM

bhandari, on 14 Feb, 2008 - 06:37 AM, said:

Quote

for (int i = 2; i <= 10; i++)
{
int digit = N % 10; // rightmost digit
sum = sum + i * digit;
N = N / 10;
}

lpporres1 Are you sure you will get what you want, irrespective of the input?

I just realized that instead of giving me a last digit that makes the whole thing divisible by 11, it does some really weird things, the thing is that i was instructed to do so, so now i'm kinda lost :S

Thank u both for your help