How to find the length of an int?

import java.util.*;

int n=787;

int l=n.length();

When I am trying to compile this I am getting the error as

int cannot be dereferenced.

And I understood that 'int' are primitives and '.' operator cannot be used with that. Now my question is how do I find the length of an an integer.

Thanks in Advance,

Ambika

Message was edited by:

Ashwathii

[424 byte] By [Ashwathiia] at [2007-11-27 4:09:40]
# 1

> Now my question is how do I

> find the length of an an integer.

Just what do you mean by the "length" of an integer? An integer takes up 32 bits (usually). There really is no length. Now if you are talking about the number of characters in the string "787", now that's a differnt story since here we are taking about strings, not ints. For instance:

int numberOfChar787 = String.valueOf(787).length();

This yields a result of 3, but again, this is a measure of a length of the String "787".

petes1234a at 2007-7-12 9:15:08 > top of Java-index,Java Essentials,Java Programming...
# 2
Length I mean is the no.of characters in the integer.n=2345; means I want the no.of digits in this, ie,I should get the answer as '4'
Ashwathiia at 2007-7-12 9:15:08 > top of Java-index,Java Essentials,Java Programming...
# 3

As petes1234 said

class intlength

{

public static void main(String args[])

{

int numberOfChar787 = String.valueOf(787).length();

System.out.println(numberOfChar787);

}

}

you can also use variable in '787'...

class intlength

{

public static void main(String args[])

{

int i = 4589;

int numberOfChar787 = String.valueOf(i).length();

System.out.println(numberOfChar787);

}

}

Message was edited by:

Reon

Reona at 2007-7-12 9:15:08 > top of Java-index,Java Essentials,Java Programming...
# 4
Why do you need the length of the base 10 representation of an integer?If this is for formatting output, like justifying numbers to the right and things like that, you can use the Formatter class or the String.format method.
jsalonena at 2007-7-12 9:15:08 > top of Java-index,Java Essentials,Java Programming...