# Thread: Im new and need help!

1. Member
Join Date
Sep 2009
Posts
16
Rep Power
0

## Im new and need help!

You guys are basically going to be my best friends this semester!! Already, I appreciate your responses!!

Im having trouble adding a couple numbers that the user inputs in a string. So for example, if the user inputs "12", what I want it to do, is manipulate it to so when it prints, it wil read 3, or 2 if I choose to multiply them. My question is, what method should I use to accomplish this? and remember I am newbie! so speaks slowly! :p

2. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10
First, be careful with your requirements. If I enter "351", is that three numbers, two numbers or one number?

3. Member
Join Date
Sep 2009
Posts
16
Rep Power
0
lol... I almost hit "report" on you thinking it was "reply."

Here's the exercise that I am trying to do:

import java.util.Scanner;

public class ISBNExercise {
public static void main(String[] args) {

//Create a Scanner
Scanner input = new Scanner(System.in);

System.out.print("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);

}
}

When the user inputs 9 numbers, I need each of those individuals numbers to represent d1,d2,d3,d4...

so if the user inputs: "394834005", each number represents d1 = 3; d2 =9, etc.
each number will then calculate into the formula that is set above. I know I need to set up a checksum and there's more to the formula... but just dissecting those numbers is what I am having trouble with.. ..I hope I didn't lose you!
Last edited by jrelvi23; 09-30-2009 at 08:57 PM.

4. did you learn about arrays?

5. Member
Join Date
Sep 2009
Posts
16
Rep Power
0
No... I do not think so. However the teacher is slowly draining my will to live so I may be wrong but I don't think so. I am basically teaching myself here. I am guessing there are other ways to do this without using arrays, though?

BTW, you guys are really fast! :D
Last edited by jrelvi23; 09-30-2009 at 09:02 PM.

6. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10
If the string represents single digit numbers then use the toCharArray method in the String class to get the individual characters.

7. Member
Join Date
Sep 2009
Posts
5
Rep Power
0
problem here:

System.out.print("Enter the first 9 numbers of an ISBN: ");
int firstNumbers = input.nextInt();
9 numbers is a way to big nummer to put in an integer.
an integer's range is : −32,768 to +32,767
anything bigger or smaller will result in an error (overflow or something?)

Maybe the sc.nextInt() is smart enough to detect this and will only read the first 4-5 digits of the full 9 digits and return that so if you type in '9876543210' the sc.nextIn() will give '9876' . and leave '543210' in the buffer..... maybe. I would have to test this to be sure.

Anyway, the point is that you can better use a String to read it in.
String string = sc.next()

The easiest way would be to indeed use arrays. The only possibility i currently see with not using arrays is using the substring method.
Java Code:
```for(int i=0 ; i<9 ; i++){
string.substring(i,i+1)
}```
(hope i didn't make any indexoutofbounds errors here)

PS: put code between code tags: [CODE] [/ CODE] (without the space in [/ CODE])

8. Senior Member
Join Date
Mar 2009
Posts
552
Rep Power
9
Originally Posted by oxano
problem here:

9 numbers is a way to big nummer to put in an integer.
an integer's range is : −32,768 to +32,767
anything bigger or smaller will result in an error (overflow or something?)

Maybe the sc.nextInt() is smart enough to detect this and will only read the first 4-5 digits of the full 9 digits and return that so if you type in '9876543210' the sc.nextIn() will give '9876' . and leave '543210' in the buffer..... maybe. I would have to test this to be sure.
since when is 9 numbers too long for an int? You're thinking of a short.

Quote from The Java Tutorials:
# short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.

# int: The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long instead.
@OP: Ase was mentioned above, use String.toCharArray() to get the individual characters, then use Integer.parseInt(c +""); (The + "" make it a string, which is what the parameter needs to be) to get the value.

Hope this helps,
SingingBoyo
Last edited by Singing Boyo; 10-01-2009 at 03:11 AM. Reason: Typos

9. Member
Join Date
Sep 2009
Posts
16
Rep Power
0
Yessssss!!! I got it to work! thanks a lot everyone! great site! :)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•