Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Trying to crunch a date to a single digit

1. ## 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

2. ## 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?

3. ## The Following User Says Thank You to KevinWorkman For This Useful Post:

twitch09 (October 14th, 2010)

4. ## 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.

```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));```

5. ## 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

6. ## Re: Trying to crunch a date to a single digit

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

7. ## Re: Trying to crunch a date to a single digit

Originally Posted by Brt93yoda
NOOOOOOO!!!!!!! I had a cool algorithm
Sorry. Hooray for spoonfeeding, I guess.

8. ## Re: Trying to crunch a date to a single digit

Originally Posted by Brt93yoda
NOOOOOOO!!!!!!! I had a cool algorithm
Complete with a nonexistant remove(...) method.

db

9. ## Re: Trying to crunch a date to a single digit

Originally Posted by Darryl.Burke
nonexistant
db
Irony. I always forget that remove is on MSL not Java.