Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Brandi Love
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Convert to a string and do a length.
 
Brandi Love
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do you do that?
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
Brandi Love
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys I ended up just doing a string length.
[ November 03, 2003: Message edited by: Brandi Love ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic