1

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?

2 Answers 2

3

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

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

5 Comments

null check pls public int compareTo(PostalCodes other) { return other==null ? 1 : city.compareTo(other.getLocation()); }
Technically correct on the null check, but that's implementation specific - if city is guaranteed to be never null, then there's no need. That, and having no null check here early on highlights places in which that assumption may be invalid.
Awesome Makoto I appreciate it
null check on other
@Makoto I did what you said, but when I tried to sort the array I got the error I showed above in my main post. I also posted my main part of my program on my edited post.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.