Question

I'm attempting to write a class that deals with locations, such as 00501 Holtsville NY 40.922326 -72.637078, 00544 Holtsville NY 40.922326 -72.637078.

I've been able to figure everything out except the Comparable part. I'm trying to modify the supplied method so that it will return the comparison based on the city name of the place.

public class PostalCodes implements Comparable {
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;
}

@Override
public int compareTo(Object arg0) {
    // Confused
    return 0;
}


}

Would it be something like

public int compareTo (Object arg0){
    if (arg0>city)
         return True;

Any and all help is much appreciated. Please keep it simple, this is the first time i've tried implementing the Comparable thing.

EDIT:

When I try to sort the array in my main as shown below....

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;
        counter++;

    }
    Arrays.sort(codesArray);
    for (int i =0;i<counter; i+=1000)
    {
        System.out.println(codesArray[i].toString());
    }

}

} I get the error

Exception in thread "main" java.lang.NullPointerException
at PostalCodes.compareTo(PostalCodes.java:69)
at PostalCodes.compareTo(PostalCodes.java:1)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at Hmwk.main(Hmwk.java:37)

What on God's green Earth happened here?

Was it helpful?

Solution

First, you should really type your Comparable. This way, you have access to the object that you want to compare against without having to cast.

public class PostalCodes implements Comparable<PostalCodes>

Then, when you implement compareTo, you only need to do it on the city field.

public int compareTo(PostalCodes other) {
    return city.compareTo(other.getLocation());
}

You may have to check for null on the object other, or city from both instances.

There may be other fields that you want to compare against, but from what you're showing us, two PostalCodes compare based on their city field. The above is the way to do that.

If you have more fields you need to compare against, you would add them in.

(You would also be better served by making your getter for city more conventional.)

OTHER TIPS

In your class compareTo method, you can always call compareTo method of String class to compare attributes of type String. I beleive city name is of type String. For integer use compareTo of Integer class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top