7 Replies Latest reply: Mar 5, 2013 1:52 AM by gimbal2

# working with numbers

Hi all, I'm new to Java programming(and programming itself) and was looking for a website such this, to get some help once in a while and find some answers to my questions...

anyway, for a start, I have a question about manipulating the numbers of a long int...like how do I choose to work with the second number from right of a 10 digits number?
I have arrays in mind...manipulating the numbers would have been easy if I choose each number go to one slot of my array ,but I don't like it this way, cuz when I run my program, it asks from the user to to put number 1, press enter, then put number 2, press enter and so on, as I wrote it here:

Scanner x = new Scanner(System.in);

int num[] = new int[10];

for( int counter=1; counter<num.length; counter++){
num[counter] = x.nextInt();

I wanted the user to put the whole number in, and then I manipulate for example 4th digit of the number from left!

And also, needed to know how I can make it to accept exactly a number that has between 8-12 digits, and if the user puts 13 digits numbers, doesn't accept it...
any help on where to start would be much appreciated :)
• ###### 1. Re: working with numbers
my first question can be said like this too: how do I convert a string of numbers into an int array?!
thanks
• ###### 2. Re: working with numbers
There's no direct method for this, but you can always first convert an int to a String, then go through each character and convert it to int (remember, '8' - '0' = 8).
You don't necessarily need an int array either, manipulating a char array is almost as easy.
• ###### 3. Re: working with numbers
oh ok...changing int to string is with this method right? static int parseInt(String x)
I didn't understand the part u said: " (remember, '8' - '0' = 8) "...I actually don't really know anything about it, if u explain a little bit it would be great...
in this case I need an int array, since I need to add or subtract the elements of the array to each other...(not sure if I can do that with a char array!or can I?)
• ###### 4. Re: working with numbers
991547 wrote:
oh ok...changing int to string is with this method right? static int parseInt(String x)
No, that's for converting a String back to an int. String.valueOf(int) can be used to convert an int into a String.
I didn't understand the part u said: " (remember, '8' - '0' = 8) "...I actually don't really know anything about it, if u explain a little bit it would be great...
The numerical value of the character '0' is 30 (or something). The numerical value of the character '1' is 31, and so on. So to convert a single digit char to an int you can do
``````    char digit = '3';
int value = digit - '0'; // value is 3``````
in this case I need an int array, since I need to add or subtract the elements of the array to each other...(not sure if I can do that with a char array!or can I?)
Well, it might be easier to do with an int array.
• ###### 5. Re: working with numbers
oh ok thanks.

so, how can I get a long int?

for an int is the method below, but I'm not sure I can use it for 16 digit integer
Scanner x = new Scanner(System.in)
int num = x.nextInt()

and if the user inputs for example : 6612319192371381318298020093, how can I find out how many digit have that number? is there a something like array.length for long integers?

Edited by: 991547 on Mar 4, 2013 12:08 PM
• ###### 6. Re: working with numbers
If you're expecting the user to input very large numbers, you'll need to read them in as Strings and convert them to BigIntegers.
BigInteger allows you to work with arbitrarily large numbers.
• ###### 7. Re: working with numbers
991547 wrote:
and if the user inputs for example : 6612319192371381318298020093, how can I find out how many digit have that number?
Get it as a String in stead of an int (because this is not an int) and look at its length() in stead. Then convert to whatever datatype you need. Apparently you can't assume it will fit in an int or even a long, so you will need to use BigInteger as suggested.