aspose file tools*
The moose likes Beginning Java and the fly likes amount of digits entered(without a tokenizer).... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "amount of digits entered(without a tokenizer)...." Watch "amount of digits entered(without a tokenizer)...." New topic
Author

amount of digits entered(without a tokenizer)....

Brandi Love
Ranch Hand

Joined: Sep 19, 2003
Posts: 133
Without using a tokenizer, how can I determine the amount of digits in an integer entered by a user? (Say the user enters 123,345. I need to somehow within the program determine that there are 6 digits)
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Convert to a string and do a length.


Please ignore post, I have no idea what I am talking about.
Brandi Love
Ranch Hand

Joined: Sep 19, 2003
Posts: 133
how do you do that?
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

With out looking in any books I would guess something like this:

I am not saying that this is the best way, only one way.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
This is probably sloppy, but I concatenate an empty string with a number to make it a string now and then:
int i = 12312312;
int digits = (""+i).length();


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Stan James:
This is probably sloppy, but I concatenate an empty string with a number to make it a string now and then
That is actually the easiest way. You could also use the static toString(int i) method of Integer:
int digits = Integer.toString(i).length();


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Brandi Love
Ranch Hand

Joined: Sep 19, 2003
Posts: 133
Thanks guys I ended up just doing a string length.
[ November 03, 2003: Message edited by: Brandi Love ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: amount of digits entered(without a tokenizer)....