Results 1 to 7 of 7
  1. #1
    sanjeeme is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Post Array Help Please

    Hi, I just beginning program with Java. now a days I working with arrays. So I had following problem still unable to solve

    I want to store someones birthday in array like below




    so I want two output to display when I changing the B'day as like below.



    1st output - addition of the birthday - (1+9+8+8) + (0+3) + (2+7) = 38

    2nd output - only addition of year - (1+9+8+8) = 26

    if some one can help me to code this using array its great help
    Last edited by sanjeeme; 06-03-2011 at 07:31 AM.

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    Can you show us what have you done so far?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    There is two ways you can do this, the first is to have an array which is filled with ints, each int representing a single digit of the birth date. So it would be 8 digits long, indices 0-3 would be the year, 4-5 would be the month, and 6-7 would be the date. This approach works easily enough, however; it may be better to have an array of Strings.

    The array of Strings would be 3 items long, index 0 is the year, 1 is the month, and 2 id the day. To add the numbers you would have to use String.substring, and parse and add the items.

    You can also use String.charAt, try and code something and see what you come up with and post your code(in code tags), if you are stuck. Include all errors(if any) when asking for help(copy/paste them, don't paraphrase).

    To use code tags, do the following:
    [code]
    YOUR CODE HERE
    [/code]

  4. #4
    sanjeeme is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default

    I just try to do this using multidimensional array. but still not got desired result

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I gave you two very simple solutions, why not try one of them? You can also just create a 3 item array of ints and use / and % to split the numbers up.

    If you can avoid multi-dimensional arrays, you should. Also, a multi-dimensional array can be thought of as an array of arrays.

  6. #6
    samanyu is offline Member
    Join Date
    Jun 2011
    Location
    Tsukuba, Japan
    Posts
    63
    Rep Power
    0

    Default

    Suppose the date is 1988-03-27. It will be stored in a single dimensional array, if you prefer a string array you will need to convert it into int by using Integer.parseInt command.
    Once you have got the int, store it in a variable, say x=19880327 and y=1988. Then use x%10, this will separate the digits, and use x/10 to remove the unwanted digit. Add the x%10 results up. You'll get the summations required.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Why use arrays?

    Anyhoo, I'd write a method to sum the digits of the int passed as a parameter. Here's a hint:
    Java Code:
    int num = 1234;
    System.out.println(num % 10);
    System.out.println(num / 10);
    Then simply call that method each time you need to sum the digits.
    Java Code:
    int year = 1988;
    int month = 12;
    int day = 22;
    int total = method(year) + method(month) + method(day);

Similar Threads

  1. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  2. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  3. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  4. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 09:01 PM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •