0
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.

2 Answers 2

1

Your code is trying to add a String to an array of PostalCodes, but you actually want to add a PostalCodes object.

Remove the call to toString():

PostalCodes code = new PostalCodes(zip,city,state,lat,longy);
codesArray[counter] = code;

BTW, I recommend renaming your class to the singular PostalCode.

Sign up to request clarification or add additional context in comments.

7 Comments

So how would I go about printing out say the first 3 elements of the array in the 095601 Amador City,CA (38.427; -120.828) format? Would a for loop with System.out.println(codesArray[i.toString()] work? Thanks for the fast reply by the way
Yeah, a for loop should be able to do that for you.
@Bohemian I tried what you said and then I tried to print out elements of the array and it's not printing anything, just running and terminating?
@Welshboy I tried doing that and it didn't print out anything. I edited my original code to display what I currently have. Any ideas where I'm going wrong here?
Well i'm trying to print the first element, and then every thousandth element
|
0

I suggest you use Collection framework Java provided.

List<PostalCodes> postalCodeList = new ArrayList<PostalCodes>(50000);

PostalCodes code = new PostalCodes(zip,city,state,lat,longy);
postalCodeList.add(code);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.