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)
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
posted
0
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
posted
0
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();