Results 1 to 7 of 7
Thread: Array Help Please
- 06-03-2011, 07:22 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
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.
- 06-03-2011, 07:27 AM #2
Can you show us what have you done so far?
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 06-03-2011, 07:28 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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]
- 06-03-2011, 07:34 AM #4
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
I just try to do this using multidimensional array. but still not got desired result
- 06-03-2011, 07:38 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 06-03-2011, 07:50 AM #6
Member
- Join Date
- Jun 2011
- Location
- Tsukuba, Japan
- Posts
- 63
- Rep Power
- 0
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.
- 06-03-2011, 07:50 AM #7
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.Java Code:int num = 1234; System.out.println(num % 10); System.out.println(num / 10);
Java Code:int year = 1988; int month = 12; int day = 22; int total = method(year) + method(month) + method(day);
Similar Threads
-
Display Array on another class after extracting from txt file and into array problem
By jonathan920 in forum NetBeansReplies: 0Last Post: 05-12-2011, 07:04 PM -
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
create a 2d char array from a 1D string array
By jschmall12 in forum New To JavaReplies: 1Last Post: 04-27-2010, 09:01 PM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks