# Trying to crunch a date to a single digit

• October 14th, 2010, 12:18 PM
twitch09
Trying to crunch a date to a single digit
An example of what I'm trying to do is I enter the date 12 / 25 / 1984

I need to add every digit like 1+2+2+5+1+9+8+4
which would equal 32, but i need to keep adding the digits until I get a single digit answer

I do not know how to separate 12 into 1 + 2 or 1984 into 1+9+8+4
• October 14th, 2010, 12:23 PM
KevinWorkman
Re: Trying to crunch a date to a single digit
Think about it: if you have 12, how do you get the '1' out of the "tens" place? It's almost exactly the same way you get the '1' out of "1984" from the "thousands" place.

After you get that, how do you isolate the rest of the number? Do you know what the modulus operator is?
• October 14th, 2010, 12:25 PM
Brt93yoda
Re: Trying to crunch a date to a single digit
What formart is the date in, is it a String?

if it's a String what you want to do is remove all of the forward slashes, and then parse each char one by one.

Code java:

```String date = "12/25/1984" date = date.replace("/","") int dateTotal = 0; for (int j=0; j < date.length; j++) dateTotal += Integer.parseInt(date.charAt(j));```
• October 14th, 2010, 12:26 PM
twitch09
Re: Trying to crunch a date to a single digit
yes i know what the modulus is and I am pretty sure I know how to do it now, thank you
• October 14th, 2010, 12:26 PM
Brt93yoda
Re: Trying to crunch a date to a single digit
Quote:

Originally Posted by KevinWorkman
Think about it: if you have 12, how do you get the '1' out of the "tens" place? It's almost exactly the same way you get the '1' out of "1984" from the "thousands" place.

After you get that, how do you isolate the rest of the number? Do you know what the modulus operator is?

NOOOOOOO!!!!!!! :( I had a cool algorithm
• October 14th, 2010, 12:32 PM
KevinWorkman
Re: Trying to crunch a date to a single digit
Quote:

Originally Posted by Brt93yoda
NOOOOOOO!!!!!!! :( I had a cool algorithm

Sorry. Hooray for spoonfeeding, I guess. :-/
• October 14th, 2010, 09:12 PM
Darryl.Burke
Re: Trying to crunch a date to a single digit
Quote:

Originally Posted by Brt93yoda
NOOOOOOO!!!!!!! :( I had a cool algorithm

Complete with a nonexistant remove(...) method.

db
• October 15th, 2010, 12:14 AM
Brt93yoda
Re: Trying to crunch a date to a single digit
Quote:

Originally Posted by Darryl.Burke
nonexistant
db

Irony. I always forget that remove is on MSL not Java.