jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes eek, help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "eek, help!" Watch "eek, help!" New topic

eek, help!

Brandi Love
Ranch Hand

Joined: Sep 19, 2003
Posts: 133
I had an assignment in which I had to write a program in which the user enters an integer between 10,000 and 99,999. The program then takes the number, strips the individual digits off (without a tokenizer) and adds them and displays the digits with the result. I figured out that you cold strip the ivdividual digits off by taking the original number and doing a %10 to get the last digit, and then to get the next digit divide the original integer by 10 (and the next by 100, the next by 1,000, etc) and again doing a %10 to get the digit. Now I have to rewrite the program so that it will do the same for ANY positive integer, and I haven't the faintest clue where to start. I know I have to rewrite the equation somehow, but thats about it. Any help would be most greatly appreciated. Here is my code for the original program:
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Take what you have and figure out how to put into a loop.
Get the number of digits of the input, and use that as the "max" for your loop.
[ November 02, 2003: Message edited by: William Barnes ]

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

Joined: Sep 19, 2003
Posts: 133
but how can I find the number of digits?
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

you could write a loop that divides by 10, and when you get a result of 0, you know you're done. i.e. inpu 123
divide by 10, get 12.
divide by 10, get 1
divide by 10, get 0. you're done. you did 3 divisions, so you have a 3 digit number.
or, you could read in a string, get the length, then translate it to an integer

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link: http://aspose.com/file-tools
subject: eek, help!
jQuery in Action, 3rd edition