• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

CompareTo() method

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
btw that last if statement is not there it is commented out. Sorry It thought I got rid of it
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't use > with a String. You have to use the compareTo method of String
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An example would be

 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if I do not have any words specific like "world" all i have is a variable called name.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If name refers to a String, then you can call the compareTo method on it.

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
grapes are vegan food pellets. Eat this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic