aspose file tools*
The moose likes Beginning Java and the fly likes length of integers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "length of integers" Watch "length of integers" New topic
Author

length of integers

Thomas Whalen
Ranch Hand

Joined: Aug 26, 2001
Posts: 123
is it possible to get the length of an integer? i have tried converting an integer to a string, then getting the length of that variable. but, it seems to be returning only the literal value of the variable.
here is my code:
// create months variable
int months = 3456;
// make String copy of months variable
String cp_months = Integer.toString(months);
// print length of cp_months
System.out.print("\nLength is " + cp_months.length);
this is boggling me. i looked at the String and Integer classes in the API, don't see anything that deals with getting the length of anything but an array.


if you don't know, then ask. if you do know, then share. love is knowledge.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Originally posted by Thomas Whalen:
is it possible to get the length of an integer? i have tried converting an integer to a string, then getting the length of that variable. but, it seems to be returning only the literal value of the variable.
here is my code:
// create months variable
int months = 3456;
// make String copy of months variable
String cp_months = Integer.toString(months);
// print length of cp_months
System.out.print("\nLength is " + cp_months.length());

So you're getting a result of 4 for this, right (after you put the parens on length()). What length are you looking for?

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Thomas Whalen
Ranch Hand

Joined: Aug 26, 2001
Posts: 123
i was only using VariableName.length ...is that why i am getting the literal value of VariableName ? as i was reading my Thinking in Java book last night i saw where Bruce was using the VariableName.length() method to get the number of integers in the variable.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
I can't get it to compile using cp_months.length.

cannot resolve symbol
symbol : variable length
location: class java.lang.String
System.out.print("\nLength is " + cp_months.length );

length is a property of an array. length() is a method of the String class.

If I use
System.out.print("\nLength is " + cp_months );
I get the literal String referenced by cp_months.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: length of integers