compareTo与整数

时间:2015-04-30 15:12:21

标签: java compareto

我正在尝试将compareTo与整数一起使用但它不会让我。请帮忙!这是我的代码:

public int compareTo(Book book)
   {
       if (this.author.compareToIgnoreCase(book.author) < 0) return -1;
       if (this.author.compareToIgnoreCase(book.author) > 0) return 1;
       if (this.author.equalsIgnoreCase(book.author))
       {
           if (this.numOfPages.compareTo(book.numOfPages) < 0) return -1;
           else if (this.lyrics.compareToIgnoreCase(song.lyrics) > 0) return 1;
           else if (this.lyrics == song.lyrics) return 0;
           else return 1;
       }
       return 0;
   }

1 个答案:

答案 0 :(得分:0)

我猜你将numOfPages定义为:

int numOfPages;  //primitive integer

如果你改为使用int的对象代表,它应该可以工作。

例如:

Integer numOfPages; //object integer