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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Trying to crunch a date to a single digit

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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. #3
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default 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));
    Last edited by Brt93yoda; October 15th, 2010 at 12:14 AM.

  5. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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. #5
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Trying to crunch a date to a single digit

    Quote Originally Posted by KevinWorkman View Post
    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. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trying to crunch a date to a single digit

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

  8. #7
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Trying to crunch a date to a single digit

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

    db

  9. #8
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Trying to crunch a date to a single digit

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

Similar Threads

  1. Declaring a date
    By Akim827 in forum Java Theory & Questions
    Replies: 7
    Last Post: October 12th, 2010, 11:47 AM
  2. sql date problem
    By realosso in forum JDBC & Databases
    Replies: 2
    Last Post: June 3rd, 2010, 08:32 AM
  3. client server program on a single machine
    By subhopam in forum Java Networking
    Replies: 1
    Last Post: December 16th, 2009, 02:02 PM
  4. How to set expiry date for cms?
    By khodam in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: December 4th, 2009, 01:15 PM
  5. Jsp code which will enable to view a single row from the Database
    By hundu in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 5th, 2009, 07:56 AM