[Homework] ISBN Check digit source code help

Need help with java code for homework assignment please

Page 1 of 1

3 Replies - 15842 Views - Last Post: 14 February 2008 - 06:40 PM Rate Topic: -----

#1 lpporres1  Icon User is offline

  • New D.I.C Head

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

[Homework] ISBN Check digit source code help

Posted 13 February 2008 - 06:58 PM

Hi everyone and thank you in advance for any kind of help you may provide :)
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) "

Any ideas about what's wrong?
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  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • 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.
Was This Post Helpful? 1

#3 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 lpporres1  Icon User is offline

  • New D.I.C Head

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

Re: [Homework] ISBN Check digit source code help

Posted 14 February 2008 - 06:40 PM

View Postbhandari, 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 :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1