aspose file tools*
The moose likes Beginning Java and the fly likes CompareTo() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "CompareTo() method" Watch "CompareTo() method" New topic
Author

CompareTo() method

Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
Hi. I'm hoping somone can help me. I have to put a compareTo() method in my program and I have one written but ehrn I test it my output message gives me an error message Stack Overflow for CompareTo method. Here is what my code looks like. I know it isn't write i'm actually surprised that I got it to compile. Any help would be appricated. Thank you!


public int compareTo(Employee anEmployee)
{
final int BEFORE = -1;
final int EQUAL = 0;
final int AFTER = 1;

Employee temp = new Employee();
if ( temp == anEmployee )
{
return EQUAL;
}

if (temp.compareTo(anEmployee)< 0)
{
return BEFORE;
}
if (temp.compareTo(anEmployee)> 0)
{
return AFTER;
}


if(temp.compareTo(anEmployee))
{

}
*/
return EQUAL;
}
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
btw that last if statement is not there it is commented out. Sorry It thought I got rid of it
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You are getting the stack overflow because what you're doing is infinite recursion. You don't want to recall the method compareTo. Instead you want to use properties of the Employee class to determine whether the parameter is "less than" or "greater than" the Employee calling compareTo
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
In these lines of code

when you call compareTo(Employee) in the if statements you are recalling this method recrsively until the JVM runs out of memory.

To implement this method you must specify under what conditions one employee will come before or after another employee. For instance here is a way to implement this comparison by employee age.


You have to make the decision what makes one employee come after another employee.

HTH
Garrett


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
I tried something like that. I tried using the name, but when I did that I could not get it to compile I recieved the following error message

Employee.java:119: operator > cannot be applied to java.lang.String,java.lang.String

I really do not know what that means. Can you tell me what the error message means?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You can't use > with a String. You have to use the compareTo method of String
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
An example would be

Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
that is where my problem comes from. I have not been able to figure out how to use the compareTo method yet. I tried to google it and I did not find anything that explained it for dummies like me can you by any chance just show me one example or tell me where I could find a plain simple example?
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
What if I do not have any words specific like "world" all i have is a variable called name.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
If name refers to a String, then you can call the compareTo method on it.

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
For the most part, you can only use > and < for comparison of primitive numeric values (there are exceptions). To compare names as you have indicated you would have to do somthing like this:

Notice that by referencing the name field (which is a String) We are calling the compareTo method of String class. One more glaring issue I see with your code is this:

Try playing aroung with it for a while and see if you can figure it out.

Garrett
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
thanks for help. I might actually understnsd what i'm doing little bit now. I will be back if I get stuck again or just to learn something new
 
Don't get me started about those stupid light bulbs.
 
subject: CompareTo() method