Computing.Net > Forums > Programming > Java Array Of Date Objects...help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Java Array Of Date Objects...help

Reply to Message Icon

Name: kev269
Date: March 2, 2005 at 08:33:56 Pacific
OS: WinXP
CPU/Ram: 512MB 3.0ghz
Comment:

Hi, I am trying to store some dates in an array and then to print them in a reverse order. But i am getting a few errors. I am not sure what i need to, perhaps an extra constructor is needed? or am i just doing the array wrong? thanks for your help.

public class array
{
public static void main(String[] args)
{
Date[] a = new Date [i];

a[0] = new Date(2, "January", 2005);
a[1] = new Date(3, "February", 2005);
a[2] = new Date(21, "March", 2005);
a[3] = new Date(28, "April", 2005);

for (int i = a.length - 1; i >= 0; i--) {
a.printDate();
}
}
}

class Date
{
int day;
String month;
int year;

Date(int d, String m, int y)
{
day = d;
month = m;
year = y;
}

public void printDate()
{
System.out.println(a[i]);
}

}



Sponsored Link
Ads by Google

Response Number 1
Name: Chi Happens
Date: March 2, 2005 at 10:42:18 Pacific
Reply:

Since you did say WHERE your error is happening, i can tell that this line is wrong:

a.printDate();

since a is an array of date objects, you cannot ask for the non indexed array element to do anything.

You should do this:
a[i].printDate();

Hope this helps,
Chi

They mostly come at night...mostly


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Java Array Of Date Objects...help

Array of objects in Java www.computing.net/answers/programming/array-of-objects-in-java/12255.html

Create an array of objects www.computing.net/answers/programming/create-an-array-of-objects/19935.html

Two dimensional array of strings www.computing.net/answers/programming/two-dimensional-array-of-strings/681.html