ForswornDragon ForswornDragon - 22 days ago 12
Java Question

Including a Date in a populated array

I am having issues putting a date into a populated array for a hierarchy tester. I am not entirely sure how to put a date in the format of mm/dd/yyyy into an array. Here is the code I have so for my tester:

import java.util.Arrays;
import java.util.Date;
public class StormHeriarchyTester
{

public static void main(String[] args)
{


TrackedStorm[] storms = new TrackedStorm[6];
storms[0] = new TrackedStorm("Kate", 11/9/2015, 23.00, -73.00, 40, 1010);
storms[1] = new TrackedStorm("Ana", 5/8/2015, 31.50 , -77.60, 30 , 1004);
storms[2] = new TrackedStorm("Matthew", 10/8/2016, 30.70, -80.60, 95, 948);
storms[3] = new TrackedStorm("Matthew", 10/6/2016, 23.00, -76.00, 100, 962);
storms[4] = new TrackedStorm("Matthew", 10/1/2016, 13.50, -71.60, 120, 949);
storms[5] = new TrackedStorm("Matthew", 10/2/2016, 13.80, -73.60, 130, 940);

System.out.println("Unsorted Storms");
for (TrackedStorm storm: storms)
System.out.println("Storm: " + storm.getName() +", "+ storm.getDate00Z() + ", " + storm.getLatitude() +", "+ storm.getLongitude() +", "+ storm.getWindspeed() +", "+ storm.getPressure());
Arrays.sort(storms);

System.out.println("Sorted Storms");
for (TrackedStorm storm: storms)
System.out.println("Storm: " + storm.getName() + ", " + storm.getDate00Z() +", "+ storm.getLatitude() +", " + storm.getLongitude() +", "+ storm.getWindspeed() +", "+ storm.getPressure());

}
}


Here is the code for TrackedStorm:

import java.util.Date;
public class TrackedStorm implements Comparable
{
private String name;
private Date date00Z;
private double latitude;
private double longitude;
private int windspeed;
private int pressure;

public TrackedStorm(String n, Date d, double lat, double longi, int w, int p)
{
name = n;
date00Z = d;
latitude = lat;
longitude = longi;
windspeed = w;
pressure = p;


}
public String toString()
{
return getClass().getName() + "Name of storm:" + name + ", ,"
+ "Date:" + date00Z + ", ,"
+ "Latitude:" + latitude + ", ,"
+ "Longitude:" + longitude + ", ,"
+ "Windspeed:" + windspeed + ", ,"
+ "Pressure:" + pressure + ", ,";

}

public String getName()
{
return name;
}

public Date getDate00Z()
{
return date00Z;
}

public double getLatitude()
{
return latitude;
}

public double getLongitude()
{
return longitude;
}

public int getWindspeed()
{
return windspeed;
}

public int getPressure()
{
return pressure;
}
public int compareTo(Object otherObject)
{
TrackedStorm other = (TrackedStorm) otherObject;
return this.windspeed - other.windspeed;
}

}

Answer

When you have a line

new TrackedStorm("Kate", 11/9/2015, 23.00, -73.00, 40, 1010);

you are passing an integer (11 divided by 9 divided by 2015 which would be 0) as the second parameter, where in the constructor you expect a Date parameter.

Replace it this way, using java.text.SimpleDateFormat:

SimpleDateFormat sdf = new SimpleDateFormat("d/M/y");
new TrackedStorm("Kate", sdf.parse("11/9/2015"), 23.00, -73.00, 40, 1010);

The parse() function takes a string in the specified format and converts it into Date instance.

The "d/M/y" passed to the SimpleDateFormat constructor says that the pattern used would be in day-month-year format.

Edit: Just noticed that you need mm/dd/yyyy format. You would have to replace "d/M/y" with "MM/dd/yyyy" or "M/d/y" then.

Comments