If you're using an integral data type, you could make use of repeated division by 10 and the modulus of division by 10. Another easy strategy would be to turn the number into a String. With a String you can easily ascertain the length and what each character is. Getting any ideas? [ March 18, 2003: Message edited by: Dirk Schreckmann ]
ya i did was wondering if there is any command or java lib which already had the feature.... (like string tokenizer and stream tokensizer) thanks for the prompt reply though i was wondering something like this just a thought but am working on it !** int number 780 int div = (int)Math.floor(Math.log(number)/Math.log(10)); System.out.println(div); **!
Joined: Nov 06, 2001
hi dave i wanted to findout the number of instances of 1 between numbers 1 -100 i.e(between 1 and 10 we have 2 instances of number 1i.e 1 and 10) intially and then later upgarding to finding any number instances specified by the user at runtime so if i use the division method it would take lot of time for the program to do the intial division and the checking that digit to the value specified by the user was just trying to get a way around that any help in achieving that would really be appreciated thank you prem
Joined: Nov 06, 2001
sorry dick didnt realise that i misspelled your name as dave my aplogies thank you prem
Prem, what solution have you come up with so far? Doing a little division doesn't take that long. Building Strings and counting characters is very easy, but would take quite a bit more memory. This is the wrong forum to speak much of performance in. If you're curious for the most efficient solution, then we should mosey on over to The Performance Forum. I wouldn't be surprised if a pattern in the count of ones appears when considering different orders of magnitude (100, 1000, 10000, etc.), but there might not be one.
Joined: Oct 08, 2001
And who is Dave? Is it Dirk and Eugene, respectively? And what is "!**" in the code below, a new JSP tag?
[ March 19, 2003: Message edited by: Eugene Kononov ]
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com