This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Need help with code!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with code!!!" Watch "Need help with code!!!" New topic
Author

Need help with code!!!

John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
There's three spots with comments, where i'm having problems with the code. For my compareTo() method my compiler says an int cannot be deferenced, than how am i supposed to compare the two integers?

David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
An integer is not an object, so you can not call methods with its reference.

Use the operators that are provided to compare primitives. You will need to use < > and == and return the appropriate values.



"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
what about returning the diffrence? Would that be a proper compareTo method? Also for the other methods I had problems with. All i wanted to do was write the least code possible for the program. But i realise that I cannot reach down the hiearchy for variables, only upwards. I'll just have to play with it.
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
In a compareTo(), there are 3 possibilities. The Object passed into compareTO() is either less then, equal to, or greater then the value of the called object. A returned value < 0 is considered to be less then, greater then is >0 and equals is ==0. This is convention and should be used unless you want to annoy people, but is not enforced by the compiler or JVM.

Returning the difference could be used, but makes things more complicated, and would only be suitable in specific cases. If you are comparing Strings, how would you calculate a difference, and what use would it be. Between integral values, it would work, but if there isn't a need to know how much they are off, then what is the point of the extra calculations?

You also need to declare that your class implements the Comparable interface.

Here is a simple Comparable class:



Using the class



Of course this means you can reference MyInt objects as Comparable references. Which can be useful sometimes.

If you are using Java 1.5 or higher you can use generics to make things simpler:



[ February 24, 2007: Message edited by: David McCombs ]
[ February 24, 2007: Message edited by: David McCombs ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by John Lockheart:
what about returning the diffrence? Would that be a proper compareTo method? ...

Yes, that's a common approach for coding compareTo using numeric values. It's a quick and easy way to get the appropriate positive, negative, or zero result.
[ February 24, 2007: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[John]: what about returning the diffrence? Would that be a proper compareTo method? ...

[marc]: Yes, that's a common approach for coding compareTo using numeric values. It's a quick and easy way to get the appropriate positive, negative, or zero result.


Unfortunately, it's an approach that sometimes leads to incorrect results.



Does this print a positive or negative number? The answer is not what most people would expect, unfortunately. A better approach is to use a few if statements as David showed above. His compareTo() method can also be written like this:


"I'm not back." - Bill Harding, Twister
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
thanks! i'll make some modifications. other than that im pretty much done. Thanks for all the input!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with code!!!
 
Similar Threads
Need help with Car Inventory Program part 3
Override toString() when you want a mere mortal to be able to read something meanningful about the..
Why is my type unexpected? required variable found value
Inheritance & Polymorphism
compareTo() method