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
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?
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));
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
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
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. :-/
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
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.