ISBN calculations

input string, have a few questions

Page 1 of 1

10 Replies - 1007 Views - Last Post: 29 September 2009 - 08:12 PM Rate Topic: -----

#1 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

ISBN calculations

Posted 29 September 2009 - 05:14 PM

I have wrote this code numerous times, and this is what I have that I think is solid. I have to use a string to input the number, and then calculate the tenth number with an equation...((num1 * 1 + num2 * 2 + num3 * 3 + num4 * 4 + num5 * 5 + num 6* 6 + num7 * 7 + num8 * 8 + num9 * 9) % 11) If the answer to that equation is 10, the tenth digit is an 'X'. Can I take a string apart and do this to the numbers?


import java.util.*;
import java.io.*;

public class ISBN
{
	public static void main(String[] args)
	{
		Scanner console = new Scanner(System.in);
		
		String num;
		int ninthDigit;
		
		System.out.println("Please enter the ISBN number(BE SURE TO PUT SPACES INBETWEEN THE NUMBERS: ");
			num = console.next();

		
		ninthDigit = console.nextInt(0) + console.nextInt(1) + console.nextInt(2) + console.nextInt(3) + console.nextInt(4) +
						console.nextInt(5) + console.nextInt(6) + console.nextInt(7);	
			
		System.out.println("You entered: " + num + " as the ISBN number.");
		
	}
}	


Or am I way off base? ~AUTO~

Is This A Good Question/Topic? 0
  • +

Replies To: ISBN calculations

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 559
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: ISBN calculations

Posted 29 September 2009 - 05:49 PM

There are numerous ways you could seperate your String, most common way problably being using a StringTokenizer. However, this would only be useful if you had some sort of deliminator. If your typing numbers in alltogether, with no spaces, you may want to consider another way.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8030
  • View blog
  • Posts: 31,182
  • Joined: 06-March 08

Re: ISBN calculations

Posted 29 September 2009 - 06:05 PM

the Scanner nextInt() method does not have a parameter
I guess you will have to read the whole ISBN as a String and decompose it after using the
Scanner nextLine() method and the String toCharArray() method
Was This Post Helpful? 1
  • +
  • -

#4 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: ISBN calculations

Posted 29 September 2009 - 06:24 PM

View Postpbl, on 29 Sep, 2009 - 05:05 PM, said:

the Scanner nextInt() method does not have a parameter
I guess you will have to read the whole ISBN as a String and decompose it after using the
Scanner nextLine() method and the String toCharArray() method


That is exactly what I need to do, but I am unsure of how to use that code...can you show me an example, or use my code to help me? TY
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8030
  • View blog
  • Posts: 31,182
  • Joined: 06-March 08

Re: ISBN calculations

Posted 29 September 2009 - 06:29 PM

String line = console.nextLine();
char[] digit = line.toCharArray();

:)
Was This Post Helpful? 1
  • +
  • -

#6 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: ISBN calculations

Posted 29 September 2009 - 06:41 PM

View Postpbl, on 29 Sep, 2009 - 05:29 PM, said:

String line = console.nextLine();
char[] digit = line.toCharArray();

:)


Okay, this is what I have....

import java.util.*;
import java.io.*;

public class ISBN
{
	public static void main(String[] args)
	{
		Scanner console = new Scanner(System.in);
		
		String num;
		int tenthDigit;
		
		System.out.println("Please enter the ISBN number(BE SURE TO PUT SPACES INBETWEEN THE NUMBERS: ");
			num = console.nextLine();
				
		tenthDigit = ((num[0] * 1) + (num[1] * 2) + (num[2] * 3) + (num[4] * 5) + (num[5] * 6) + (num[6] * 7) +
							(num[7] * 8) + (num[8] * 9) % 11);
							
			if (tenthDigit == 10)
			{
				tenthDigit = 'X';
			}
			
			else if (tenthDigit != 10)
			{
				tenthDigit = tenthDigit;
			}		
							
			
		System.out.println("The tenth digit is: " + tenthDigit);
		
	}
}	


And these are my errors...


----jGRASP exec: javac -g C:\Users\Christopher\Documents\ISBN.java

ISBN.java:22: array required, but java.lang.String found
tenthDigit = ((num[0] * 1) + (num[1] * 2) + (num[2] * 3) + (num[4] * 5) + (num[5] * 6) + (num[6] * 7) +
^
ISBN.java:22: array required, but java.lang.String found
tenthDigit = ((num[0] * 1) + (num[1] * 2) + (num[2] * 3) + (num[4] * 5) + (num[5] * 6) + (num[6] * 7) +
^
ISBN.java:22: array required, but java.lang.String found
tenthDigit = ((num[0] * 1) + (num[1] * 2) + (num[2] * 3) + (num[4] * 5) + (num[5] * 6) + (num[6] * 7) +
^
ISBN.java:22: array required, but java.lang.String found
tenthDigit = ((num[0] * 1) + (num[1] * 2) + (num[2] * 3) + (num[4] * 5) + (num[5] * 6) + (num[6] * 7) +
^
ISBN.java:22: array required, but java.lang.String found
tenthDigit = ((num[0] * 1) + (num[1] * 2) + (num[2] * 3) + (num[4] * 5) + (num[5] * 6) + (num[6] * 7) +
^
ISBN.java:22: array required, but java.lang.String found
tenthDigit = ((num[0] * 1) + (num[1] * 2) + (num[2] * 3) + (num[4] * 5) + (num[5] * 6) + (num[6] * 7) +
^
ISBN.java:23: array required, but java.lang.String found
(num[7] * 8) + (num[8] * 9) % 11);
^
ISBN.java:23: array required, but java.lang.String found
(num[7] * 8) + (num[8] * 9) % 11);
^
8 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

Where would I put the other statement that you gave me? And what is going on with my compiler?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8030
  • View blog
  • Posts: 31,182
  • Joined: 06-March 08

Re: ISBN calculations

Posted 29 September 2009 - 06:48 PM

Aie ! Aie ! Aie !
You are really lost num is a String you cannot num(0) or num(1)

// read a line as a String with the scanner
String line = console.nextLine();
// translate thius line into a char array
char[] digit = line.toCharArray();
// make an array of it to work with int
int[] num = new int[digit.length];
// convert char (which are ASCII character into int)
for(int i = 0; i < digit.length; i++) {
   num[i] = (int) digit[i];	 // cast char to int
   num[i] -= (int) '0';		 // translate from ASCII to int
}

// now you can play with num[0], num[1], ......


Was This Post Helpful? 1
  • +
  • -

#8 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: ISBN calculations

Posted 29 September 2009 - 07:28 PM

View Postpbl, on 29 Sep, 2009 - 05:48 PM, said:

Aie ! Aie ! Aie !
You are really lost num is a String you cannot num(0) or num(1)

// read a line as a String with the scanner
String line = console.nextLine();
// translate thius line into a char array
char[] digit = line.toCharArray();
// make an array of it to work with int
int[] num = new int[digit.length];
// convert char (which are ASCII character into int)
for(int i = 0; i < digit.length; i++) {
   num[i] = (int) digit[i];	 // cast char to int
   num[i] -= (int) '0';		 // translate from ASCII to int
}

// now you can play with num[0], num[1], ......



Thank you very much...I just have a few questions though. What does the '-=' do? Also, my math is screwy on this... I want to learn this, that is why I am asking...I am not trying to ignorant, just want to understand this inside and out. TY ~AUTO~

This is what I have ....

import java.util.*;
import java.io.*;

public class ISBN
{
	public static void main(String[] args)
	{
		Scanner console = new Scanner(System.in);
		
		System.out.println("Please enter the ISBN number: ");
			String line = console.nextLine();
			
		char[] digit = line.toCharArray();
		
		int[] num = new int[digit.length];

		for(int i = 0; i < digit.length; i++)
		{
			num[i] = (int) digit[i];
			num[i] -= (int) '0'; 
		}

	int tenthDigit = ((num[0] * 1) + (num[1] * 2) + (num[2] * 3) + (num[4] * 5) + (num[5] * 6) + (num[6] * 7) +
							(num[7] * 8) + (num[8] * 9) % 11);
							
		if (tenthDigit == 10)
		{
			tenthDigit = 'X';
		}
			
		else if (tenthDigit != 10)
		{
			tenthDigit = tenthDigit;
		}		
							
			
		System.out.println("The tenth digit is: " + tenthDigit);
		
	}
}


It compiles great... Here is my compiler output....

----jGRASP exec: javac -g C:\Users\Christopher\Documents\ISBN.java


----jGRASP: operation complete.

And here is my program output....


----jGRASP exec: java ISBN

Please enter the ISBN number:
013604258
The tenth digit is: 95

----jGRASP: operation complete.

I don't know what is going on with it, but it needs to read like a real ISBN. It needs to have a single tenth digit or the 'X', I know that I can use the printf and the %.2f for decimal places, but is there something that I can use predecimal point? Or is my math just wrong? Am I not seeing something? TY again for all of the help!!!!
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8030
  • View blog
  • Posts: 31,182
  • Joined: 06-March 08

Re: ISBN calculations

Posted 29 September 2009 - 07:40 PM

		if (tenthDigit == 10)
		{
			tenthDigit = 'X';
		}
			
		else if (tenthDigit != 10)	   // else if(thenthDigit == 10) is surely thenthDigit != 10 kind a useless if here
		{										// 
			tenthDigit = tenthDigit;	 // unless I really miss the point this is also useless
		}										// so the 4 last lines are really useless


Was This Post Helpful? 1
  • +
  • -

#10 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: ISBN calculations

Posted 29 September 2009 - 07:57 PM

View Postpbl, on 29 Sep, 2009 - 06:40 PM, said:

		if (tenthDigit == 10)
		{
			tenthDigit = 'X';
		}
			
		else if (tenthDigit != 10)	   // else if(thenthDigit == 10) is surely thenthDigit != 10 kind a useless if here
		{										// 
			tenthDigit = tenthDigit;	 // unless I really miss the point this is also useless
		}										// so the 4 last lines are really useless



Well I have never been told that I have underbuilt anything, motors, bathrooms, sheds, etc,...so I figured if it was in there, it was there. It is all figured out now,(BIG THANKS TO pbl), I got my calculations messed up because I missed a set of parens....my fault, lol. But, I know that the -= is the subtraction assignment right to left, but I don't see how it plays in this with the '0'...can you further explain please...
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8030
  • View blog
  • Posts: 31,182
  • Joined: 06-March 08

Re: ISBN calculations

Posted 29 September 2009 - 08:12 PM

  if (tenthDigit == 10)
		{
			tenthDigit = 'X';
		}



thenthDigit is an int
if you assign it 'X' it will take the ASCII value of character 'X'
you can print it as a char thow

don't really understand your "algorithm" thow

assume that you ISBN first digits are 11111111

1 * 1 + 1 * 2 + 1* 3 * 1 * 4... is surely higher than 10
how can you expect the sum to be 10 ?

oups... missed the % 11 sorry

First use a loop

int total = 0;
for(int i = 0; i < 9; i++)
   total += (num[i] * (i+1));

int mod = total % 11;
char lastDigit;
if(mod == 10)
   lastDigit = 'X';
else
   lastDigit = '0' + mod;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1