File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes comparable interface help? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "comparable interface help?" Watch "comparable interface help?" New topic
Author

comparable interface help?

Taylor Thibodeaux
Greenhorn

Joined: Dec 02, 2012
Posts: 11
Hi, I'm making a program comparing heights using a compareTo() method and using comparable interface for the first time. I keep getting the error message "Height is not abstract and does not override abstract method compareTo(Height) in Comparable" and I just don't know why! Here's my code, and I would love help!


thanks!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4338
    
    7

The problem is that Java is case-sensitive. Comparable has a method compareTo that you need to implement, but you've implemented a method CompareTo.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40

Taylor Thibodeaux wrote:Hi, I'm making a program comparing heights using a compareTo() method and using comparable interface for the first time. I keep getting the error message "Height is not abstract and does not override abstract method compareTo(Height) in Comparable" and I just don't know why! Here's my code, and I would love help!


thanks!


Java is case sensitive -- and the name of your method versus the case of the name of the method required by the Comparable interface, don't match.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Taylor Thibodeaux
Greenhorn

Joined: Dec 02, 2012
Posts: 11
Oh wow. Such a simple and overlooked fix. Thanks to both of you for the lightning fast reply! I was starting to get worried.
Taylor Thibodeaux
Greenhorn

Joined: Dec 02, 2012
Posts: 11
One more small problem, sorry heheh. I made a separate "Sorting" class that gets input from a txt file and outputs it onto another. In this class, it stores the height values in an array list. When I look at the new text file it generates, all the values are 0, even though the original text file had distinct single digit height numbers. I'm not even sure why an exception is not being thrown that was written in the original height class when both values are zero either. The feet and inches variables are correctly using the values, but its not making a height object correctly, and im not sure why.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

The constructor that starts at line 25 never assigns any values.

Also, not a compiler error, but isn't 12 inches equal to 1 foot? So shouldn't the check be i >= 12 instead of i > 12?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4338
    
    7

By the way, you can simplify your compareTo method. It doesn't have to return 1 or -1 depending on the order, it just needs to return a position or negative integer. So this bit:
can be replaced by this:

And you can do something similar with the feet. In fact, given the constraint on inches you can actually just write the entire method as:
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3573
    
  14

Actually, you might want to make that
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4338
    
    7

If these are heights of people, it's seriously unlikely to matter .

Taylor - what Stephan's getting at (I assume) is that if the value of feet gets very large, feet*12 could possibly get too large to fit into an int variable. In that case you'll get an "overflow", and unexpected results. Converting it to a long first, and then only converting the final value back to an int, will avoid that problem in some cases. But not all (e.g. feet very large and h.feet very small), so if there was any danger of overflow that I'd actually go back to comparing them separately.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Taylor Thibodeaux wrote:Oh wow. Such a simple and overlooked fix. Thanks to both of you for the lightning fast reply! I was starting to get worried.

Note: The @Override annotation exists exactly to catch errors such as this one. If you would have used it, the compiler would have given you an error: "The method CompareTo does not override a superclass method".


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3573
    
  14

Matthew Brown wrote:If these are heights of people, it's seriously unlikely to matter .

Taylor - what Stephan's getting at (I assume) is that if the value of feet gets very large, feet*12 could possibly get too large to fit into an int variable. In that case you'll get an "overflow", and unexpected results. Converting it to a long first, and then only converting the final value back to an int, will avoid that problem in some cases. But not all (e.g. feet very large and h.feet very small), so if there was any danger of overflow that I'd actually go back to comparing them separately.


Actually, it will work for all cases, because feet is cast to long, and afterwards the rest of the operands will be promoted to long automatically.

But yeah, people are unlikely to get that tall. It's not a good reason not to write robust code, though :P
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4338
    
    7

Stephan van Hulst wrote:Actually, it will work for all cases, because feet is cast to long, and afterwards the rest of the operands will be promoted to long automatically.


I know. But what if the final result is too big to be cast back to an int accurately? E.g. feet = Integer.MAX_VALUE, and all the other values are zero?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3573
    
  14

Woops. Let's make that:
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: comparable interface help?
 
Similar Threads
Comparable interface
Still not getting working of compareTo() method
Urgent need Help Sorting Collections
Implementing Comparable With a Generic Type
Converting long distances/times in seconds to seperate parts in a String