比较compareTo功能

时间:2013-04-07 21:22:46

标签: java string comparable compareto

我正在尝试比较两个街道地址的可比较功能。我正在研究地址的String(街道名称)。到目前为止,我有以下代码。我最终希望能够比较街道号码,但我希望能够首先修复此代码。每当我尝试编译此代码时,都会收到以下错误:

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);
}
}

2 个答案:

答案 0 :(得分:3)

正如您所说,问题在于:

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

您无法减去字符串。获得我认为你正在寻找的结果的方法是:

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

显然,这假设stNameString。如果不是,您必须在compareTo的类中实现stName,正如我在编辑中所做的那样。您在编辑时报告的问题是该课程如下:

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

但是,您尝试按如下方式实例化该类:

StreetName fred = new StreetName("name");

您只发送构造函数String,而不是int。假设数字很重要,您需要将其更改为:

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

答案 1 :(得分:0)

从另一个字符串中减去一个字符串是什么意思?这没有多大意义。而是如何在那个位置调用String自己的compareTo(...)方法。此外,==运营商不应该在那里:

int compareName = stName.compareTo(street.stName);

此外,您的compareTo方法被定义为返回一个int,因此最好返回该int。现在它没有返回任何东西。