Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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]);
}}

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,
ChiThey mostly come at night...mostly

![]() |
![]() |
![]() |

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