4

e.g. for int n = 1234 I could create a string (s.valueOf(n)), then I would define the array like this:

int[] array = new int[s.length()]; //this allocates memory for an array with 4 elements

Is there any other way to do this without using a string and only integers?

constant
  • 113
  • 5
  • Sorry? Can you please elaborate a bit more? – Freak May 07 '13 at 10:11
  • Getting the length in digits of a number is a well known problem for programming students. I am sure you can google it pretty quickly. – SJuan76 May 07 '13 at 10:12
  • @freak - What else can I say? – constant May 07 '13 at 10:12
  • @freak he basically wants to get the size of n and initialize his array. like int n= 1234; here you have 4 digits, thus length would be 4 – PermGenError May 07 '13 at 10:12
  • int is primitive and I think doesn't have a method for finding its length – Oneb May 07 '13 at 10:13
  • 1
    @constant you should say like PermGenError elaborate it. – Freak May 07 '13 at 10:14
  • Yes, you could write yourself a little method to work this out without using Strings, but is this a homework task? Don't want to just go giving you the answer without you showing us what you've tried first. – Quetzalcoatl May 07 '13 at 10:14
  • Good, I found this: http://stackoverflow.com/questions/1306727/way-to-get-number-of-digits-in-an-int – constant May 07 '13 at 10:15
  • 1
    Just simple mathematics, you need the concept of logarithm. As the given answer suggests. – mike May 07 '13 at 10:18

5 Answers5

13

You can use Math#log10 to find the number of digits.

numOfDigits = (int)(Math.log10(n)+1);

Now you do:

int[] array = new int[numOfDigits];

Note that if n = 1999, numOfDigits will be 4. So you're allocating a memory for 4 integers and not 1999 integers.

But be careful, while reading the documentation of the method, you'll note:

If the argument is positive zero or negative zero, then the result is negative infinity.

Maroun
  • 94,125
  • 30
  • 188
  • 241
5

I assume you are talking about Java, so:

 int value = myValue;
 for (int noOfDigits = 1; Math.abs(value) >= 1; ++noOfDigits) {
     value /= 10;
 }

 int[] array = new int[noOfDigits];

This does not include space for the leading sign if the number is negative, but you can easily test this condition and increment noOfDigits by one.

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
2

Use log function to find the no. of digits.

int size = (int)Math.log10(1234)+1;
int[] array = new int[size];
Vineet Singla
  • 1,609
  • 2
  • 20
  • 34
0

According to my understanding you can do the following to get count of digits

    int n = 12345;
    int count = 1;
    while(n>10){
         n = n/10;
         count++; 
    }
    int[] array = new int[count];
Bader Javaid
  • 41
  • 1
  • 2
  • 9
0
if (n>0){
    numberOfDigets = (int)(Math.log10(n)+1);
} 
else if (n < 0){
    numberOfDigets = (int)(Math.log10(Math.abs(n))+1);
} else {
    numberOfDigets = 1;
}

If n is bigger then zero use the Math.log10 function as Maroun Maroun wrote. If the n is less the zero use the Math.abs function to get a positiv value. If you want to allocate space for the - the add 2 instead of 1. The else clause is for the case when n is zero and sets numberOfDigets to 1.

If an extra call to a java function don't matter use this code. if (n != 0){ numberOfDigets = (int)(Math.log10(Math.abs(n))+1); } else { numberOfDigets = 1; }

Math.abs(n) will always return a positiv version of n. The value to watch out for is the Integer.min_value beacuse that value will still be negativ, but that is another question.

Mattias
  • 1,331
  • 3
  • 11
  • 11