posted 8 years ago

HEEELLLOOOOOOOOOOOOOO!!! I am sorry. Was that too loud? Was I suppose to whisper? I don't know. I'm new. Anyway I need help this semester so I hope you guys bear with me. I have a horrible teacher and is making me lose interest as I typing. Hopefully you guys can help.

Basically what I am trying to to is adding numbers in a string that the user inputs.

Here's my exercise:

I am trying to calculate the checksum for an ISBN number. There's more into the formula than what the program below has, but that isn't the problem. The problem is dissecting each individual number the user inputs so it will represent d1,d2,d3,d4 and then fit into the formula.

Here's what I have so far...

import java.util.Scanner;

public class ISBNExercise {

public static void main(String[] args) {

//Create a Scanner

Scanner input = new Scanner(System.in);

System.out.printf("Enter the first 9 numbers of an ISBN: ");

int firstNumbers = input.nextInt();

//int calcFirstNumbers = (d1 * 10 + d2 * 9 + d3 * 8 + d4 * 7 + d5 * 6 + d6 * 5 + d7 * 4 + d8 * 3 + d9 * 2);

//System.out.println("Adding the first 9 numbers equals to: " + calcFirstNumbers);

System.out.print("Your 10-digit ISBN Number is: " + firstNumbers);

}

}

I appreciate the help! Thanks!

Basically what I am trying to to is adding numbers in a string that the user inputs.

Here's my exercise:

I am trying to calculate the checksum for an ISBN number. There's more into the formula than what the program below has, but that isn't the problem. The problem is dissecting each individual number the user inputs so it will represent d1,d2,d3,d4 and then fit into the formula.

Here's what I have so far...

import java.util.Scanner;

public class ISBNExercise {

public static void main(String[] args) {

//Create a Scanner

Scanner input = new Scanner(System.in);

System.out.printf("Enter the first 9 numbers of an ISBN: ");

int firstNumbers = input.nextInt();

//int calcFirstNumbers = (d1 * 10 + d2 * 9 + d3 * 8 + d4 * 7 + d5 * 6 + d6 * 5 + d7 * 4 + d8 * 3 + d9 * 2);

//System.out.println("Adding the first 9 numbers equals to: " + calcFirstNumbers);

System.out.print("Your 10-digit ISBN Number is: " + firstNumbers);

}

}

I appreciate the help! Thanks!

posted 8 years ago

Hi Mel, welcome to javaranch.

Please Use Code Tags when you post a source code. You can edit your message using button and then add code tags to it...

Please Use Code Tags when you post a source code. You can edit your message using button and then add code tags to it...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links

posted 8 years ago

I think you have the answer to your question here already? Unless I understand something wrong

With that statement, you read ONE int. So if you need nine numbers, perform that statement nine times (hint: for loop), perhaps reading the numbers into an array of int. Then just add them up?

And btw. please use code tags next time ;)

Mel Ram wrote:

I think you have the answer to your question here already? Unless I understand something wrong

With that statement, you read ONE int. So if you need nine numbers, perform that statement nine times (hint: for loop), perhaps reading the numbers into an array of int. Then just add them up?

And btw. please use code tags next time ;)

posted 8 years ago

If you are to read the ISBN number as a single integer, you will have to "parse" it for the individual digits. This can be done

with a combination of integer ( / ) and remainder ( % ) division.

For example, if I have the four-digit number 9999, I can obtain the left-most digit by 9999 / 1000 = 9. Then I can "remove"

the left-most digit by 9999 % 1000 = 999. This process can be repeated in a loop to obtain each digit. Since you know how

many digits are to be in an ISBN number, you know how many times to iterate through the loop and with what place value

to begin. What is more interesting is if you don't know beforehand how many digits are in the number you are parsing. Then you have to iterate through a loop, each time dividing by a greater multiple of 10, until the result is 0. Then you know

you have gone one place value too far.

with a combination of integer ( / ) and remainder ( % ) division.

For example, if I have the four-digit number 9999, I can obtain the left-most digit by 9999 / 1000 = 9. Then I can "remove"

the left-most digit by 9999 % 1000 = 999. This process can be repeated in a loop to obtain each digit. Since you know how

many digits are to be in an ISBN number, you know how many times to iterate through the loop and with what place value

to begin. What is more interesting is if you don't know beforehand how many digits are in the number you are parsing. Then you have to iterate through a loop, each time dividing by a greater multiple of 10, until the result is 0. Then you know

you have gone one place value too far.