This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparing help!

 
christopher persinger
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone.. i have 2 questions/problems:

1. posted below is some code from a program i am working on. i am wondering if there is a more efficient way to doing the compareTo method than the way i am doing it by creating 2 strings with all of the information in it.

2. i am having a problem creating a workbench for this class. everything is compiling fine but when i create a instance of this class, i am having a hard time entering a value for Date. i have tried entering the date in many different ways but i keep getting an error that reads: type int cannot be converted to type java.util.Date. any suggestions on what to do? thanks in advance!!

 
Norm Radder
Ranch Hand
Posts: 728
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compareTo() returns an int. 0=equal, - lesser and + greater
For your application, what is the meaning of lesser or greater?
How/for what are you using this method?

Could you post the statement that gives the compile error with the definitions for the variables used?

I image something like this:
Date aDate = 4;
or int x = aDate;
 
christopher persinger
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the meaning of the lesser and greater: i am making a program that will read in 3 text files and build 3 lists accordingly. the lists will be arranged in alphabetical order based on how they are read thru the 3 different classes. the order class that im working on now will, for instance, read in the orders text file and arrange everything in the text file in an alphabetical list. so basically i am trying to set up a compareTo method that will do just this. it needs to compare based on this order: 1. date, 2. last name, 3. first name, 4. title

oh and about the definitions for the variables used, i have not set up any date variables yet. this will happen when the text file is read in and then the date will be processed. i was just trying to set up a workbench to see if my compareTo method was producing the right integer (positive, negative, or zero) for a couple 'made up' instances.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're comparing these objects based on date, last name, first name, and title -- in that order -- then one approach is...
  • Compare the dates using Date's compareTo method. If the result is not zero, then return that result. But if the result is zero, then...
  • Compare the last names using String's compareTo method. If the result is not zero, then return that result. But if the result is zero, then...
  • Compare the first names...
  • Compare the titles...
  • Return zero.
  •  
    christopher persinger
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    how does this look??

     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Take another look at those "else" statements. Consider (for example) what would happen if dates are equal, and last names are equal, but first names are different.
     
    christopher persinger
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    could i use this statement for the compareTo??

     
    Stan James
    (instanceof Sidekick)
    Ranch Hand
    Posts: 8791
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Could you get a +1 for one compare and a -1 for another and sum them up to look equal?

    Back to Marc's suggestion, I have some of these that look like:
     
    christopher persinger
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    no i couldnt sum the 2 up .. i need to either get a negative integer, positive integer, or zero based on all four fields (date, last name, first name, and title).. basically i need to be able to put them in alphabetical order based on these fields. so, in that case.. could i just use this???

     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by christopher persinger:
    ... could i just use this??? ...

    Yes, that would work, but it could be simpler. Keeping it simple makes troubleshooting and maintenance easier.

    Look at Stan's code above, and notice how it updates the result variable as needed (in a straightforward way, without any "else" statements), then uses a single "return" statement at the end.
     
    christopher persinger
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    yeah but does stan's code break out if one of the lines does not equal zero? like say date and lastname are equal to 0 but then firstname is different, will it break out and return the result? or what?
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by christopher persinger:
    yeah but does stan's code break out if one of the lines does not equal zero? like say date and lastname are equal to 0 but then firstname is different, will it break out and return the result? or what?

    It does not "break out" of the method early, as it might if there were return statements after each comparison (returning if result != 0). However, as soon as a comparison in Stan's code returns a non-zero value, the remaining comparisons are skipped, and execution proceeds to the return statement at the end.

    Specifically, under the scenario you raised above....
  • Equal dates would assign 0 to result.
  • Because result == 0, last names would be compared.
  • Equal last names would re-assign 0 to result.
  • Because result == 0, first names would be compared.
  • Different first names would assign a non-zero int to result.
  • Now, because result != 0, titles would not be compared, and result would keep its non-zero value.
  • The return statement would return the int given by the first name comparison.
  •  
    Stan James
    (instanceof Sidekick)
    Ranch Hand
    Posts: 8791
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Inded I left some extra tests in there - if the first comparison is not zero there's no logical reason to test result == 0 over and over - just for easy reading. You could reduce the extra tests with early return or with nested ifs or &&.

    All icky in one way or another, no? Choose your poison or something like that.
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Stan James:
    ... Choose your poison or something like that.

    I choose your first version for easy reading and maintenance. I wouldn't worry about a couple of extra int comparisions.

     
    christopher persinger
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    yeah, i am just going to stick with Stan's first code with the if statements and 1 return statement. thanks everyone for your help!!!
     
    christopher persinger
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ahhhh another question.. on my compareTo method, it states (Order anOrder).. i need it to be (Object anOrder).. but when i change it, it doesnt compile.. what do i need to do??
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by christopher persinger:
    ahhhh another question.. on my compareTo method, it states (Order anOrder).. i need it to be (Object anOrder).. but when i change it, it doesnt compile.. what do i need to do??

    If you pass a reference of type Order to a method that accepts an argument of type Object, then the Order reference is automatically upcast to type Object through "method call conversion." In order to treat this reference as an instance of Order within the method, you need to downcast it back to type Order.

    Note: To be safe about this, you would also include some code to verify that the reference you have is, in fact, an instanceof Order before trying to cast it.
     
    Ernest Friedman-Hill
    author and iconoclast
    Marshal
    Pie
    Posts: 24208
    35
    Chrome Eclipse IDE Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by marc weber:

    Note: To be safe about this, you would also include some code to verify that the reference you have is, in fact, an instanceof Order before trying to cast it.


    It's legal, and common, for compareTo() to throw ClassCastException if the argument is of the wrong type -- the method in the Comparable interface actually declares this exception. So really, just the cast is OK.
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Ernest Friedman-Hill:
    ... It's legal, and common, for compareTo() to throw ClassCastException if the argument is of the wrong type -- the method in the Comparable interface actually declares this exception. So really, just the cast is OK.

    Good point! I had forgotten that this was declared.
     
    Stan James
    (instanceof Sidekick)
    Ranch Hand
    Posts: 8791
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is veering off from your problem, but when you used the word "order" the thought "sort order" popped up in my aged brain. What if you wanted to sort something ascending by department, descending by total sales?

    See how my field2 and other.field2 are reversed? Fun with sorting.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic