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 :(-::(-:
Can you show us what have you done so far?
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:
YOUR CODE HERE
I just try to do this using multidimensional array. but still not got desired result
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.
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.
Why use arrays?
Anyhoo, I'd write a method to sum the digits of the int passed as a parameter. Here's a hint:
Then simply call that method each time you need to sum the digits.
int num = 1234;
System.out.println(num % 10);
System.out.println(num / 10);
int year = 1988;
int month = 12;
int day = 22;
int total = method(year) + method(month) + method(day);