public class PostalCodes {
private String city;
private double latitude;
private double longitude;
private String zip;
private String state;
public PostalCodes(String aZip, String aCity, String aState, double aLatitude, double aLongitude)
{
city = aCity;
latitude = aLatitude;
longitude = aLongitude;
zip = aZip;
state = aState;
}
void setZip(String aZip)
{
zip=aZip;
}
void setState(String aState)
{
state=aState;
}
void setLocation(String aCity)
{
city = aCity.trim();
}
void setLatitude(double lat)
{
latitude = lat;
}
void setLongitude(double long1)
{
longitude = long1;
}
public String getState()
{
return state;
}
public String getZip()
{
return zip;
}
public String getLocation()
{
return city;
}
public double getLatitude()
{
return latitude;
}
public double getLongitude()
{
return longitude;
}
public String toString()
{
String result = String.format("%s %s,%s (%1.3f; %1.3f)",zip, city, state, latitude,longitude);
return result;
}
} Above is my class i'm trying to make an array of 50000 of and below is my main...
public class Hmwk {
public static void main(String[] args) throws IOException {
URL url = new URL("http://noynaert.net/zipcodes.txt");
Scanner input=new Scanner (url.openStream());
int counter =0;
final int MAX_SIZE=50000;
PostalCodes[] codesArray= new PostalCodes[50000];
while (input.hasNextLine() && counter < MAX_SIZE)
{
String line=input.nextLine();
String[] tokens;
tokens = line.split("\t");
if (tokens.length != 5)
{
continue;
}
String zip=tokens[0];
String city=tokens[1];
String state=tokens[2];
double lat=Double.parseDouble(tokens[3]);
double longy=Double.parseDouble(tokens[4]);
PostalCodes code=new PostalCodes(zip,city,state,lat,longy);
codesArray[counter]= code; //Error here
//System.out.println(code.toString());
counter++;
}
for (int i =0;i<counter; i+=1000)
{
System.out.println(codesArray[i].toString());
}
}
I'm attempting to save the first 50000 entries as 095601 Amador City,CA (38.427; -120.828),095604 Auburn,CA (39.106; -120.536), 095605 West Sacramento,CA (38.592; -121.528) etc. in my array. I was able to print out the format I want, but I can't figure out how to add it to an array. Thanks for your time.