Search code examples
javastringcomparablecompareto

Comparable compareTo function


I'm trying to have a Comparable function compareTo two street addresses. I'm working on the String (street name) of the address at the moment. So far, I have the following code. I eventually want to be able to compare the street numbers too, but I'd like to be able to get this code fixed first. Whenever I try to compile this code, I get the following error:

StreetAddress.java:45: error: constructor StreetAddress in class StreetAddress cannot be applied to given types;
    StreetAddress add1 = new StreetAddress("cartesian road");
                         ^
  required: no arguments
  found: String
  reason: actual and formal argument lists differ in length
StreetAddress.java:46: error: constructor StreetAddress in class StreetAddress cannot be applied to given types;
    StreetAddress add2 = new StreetAddress("cartesian road");
                         ^
  required: no arguments
  found: String
  reason: actual and formal argument lists differ in length
StreetAddress.java:47: error: constructor StreetAddress in class StreetAddress cannot be applied to given types;
    StreetAddress add3 = new StreetAddress("n kings street");
                         ^
  required: no arguments
  found: String
  reason: actual and formal argument lists differ in length
StreetAddress.java:48: error: constructor StreetAddress in class StreetAddress cannot be applied to given types;
    StreetAddress add4 = new StreetAddress("pioneer parkway");
                         ^
  required: no arguments
  found: String
  reason: actual and formal argument lists differ in length
StreetAddress.java:49: error: constructor StreetAddress in class StreetAddress cannot be applied to given types;
    StreetAddress add5 = new StreetAddress("starry avenue");
                         ^
  required: no arguments
  found: String
  reason: actual and formal argument lists differ in length
5 errors

public class StreetAddress implements Comparable<StreetAddress>
{
protected int num;
protected String stName;

public void StreetAddress(int n, String s){
num = n;
stName = s;
}

public int getNum(){
    // returns the street number
    return num;
}

public String getName(){
    // returns the street name
    return stName;
}

public int compareTo(StreetAddress street) throws ClassCastException{
    // exception prevents crash if an address is not compared to
    // another address
    StreetAddress x = (StreetAddress) street;
    int compareName = this.stName.compareTo(street.stName); 
    if (compareName < 0){
        // first address comes after compared address
        System.out.println("test<0");
    }

    else if (compareName == 0){
        // same address
        System.out.println("test equal");
    }

    else{
        // first address comes before compared address
        System.out.println("test > 0");
    }

    }

public static void main(String args[])
{
StreetAddress add1 = new StreetAddress("cartesian road");
StreetAddress add2 = new StreetAddress("cartesian road");
StreetAddress add3 = new StreetAddress("n kings street");
StreetAddress add4 = new StreetAddress("pioneer parkway");
StreetAddress add5 = new StreetAddress("starry avenue");
add1.compareTo(add2);
add4.compareTo(add1);
add3.compareTo(add3);
add2.compareTo(add5);
}
}

Solution

  • The issue, as you note, is with:

    compareName == this.stName - (street.stName);
    

    You can't subtract strings. The way to get the result I think you're looking for is:

    int result = this.stName.compareTo(street.stName);
    

    Obviously this assumes that stName is a String. If not you will have to implement compareTo within the class that stName is, as I see you have done in an edit. The issue you report with your edit is that the class is as follows:

    class StreetName {
        // Constructor
        StreetName(int n, String st) {
             // code
        }
    }
    

    However, you are trying to instantiat the class as follows:

    StreetName fred = new StreetName("name");
    

    You are only sending the constructor a String, and not an int with it. Assuming the number is important, you need to change that to:

    StreetName fred = new StreetName(number, "name");