This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes comparing help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "comparing help!" Watch "comparing help!" New topic
Author

comparing help!

christopher persinger
Greenhorn

Joined: Aug 23, 2005
Posts: 20
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

Joined: Aug 10, 2005
Posts: 685
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

Joined: Aug 23, 2005
Posts: 20
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

Joined: Aug 31, 2004
Posts: 11343

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.


  • "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
    christopher persinger
    Greenhorn

    Joined: Aug 23, 2005
    Posts: 20
    how does this look??

    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    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

    Joined: Aug 23, 2005
    Posts: 20
    could i use this statement for the compareTo??

    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    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:


    A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
    christopher persinger
    Greenhorn

    Joined: Aug 23, 2005
    Posts: 20
    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

    Joined: Aug 31, 2004
    Posts: 11343

    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

    Joined: Aug 23, 2005
    Posts: 20
    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

    Joined: Aug 31, 2004
    Posts: 11343

    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

    Joined: Jan 29, 2003
    Posts: 8791
    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

    Joined: Aug 31, 2004
    Posts: 11343

    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

    Joined: Aug 23, 2005
    Posts: 20
    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

    Joined: Aug 23, 2005
    Posts: 20
    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

    Joined: Aug 31, 2004
    Posts: 11343

    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

    Joined: Jul 08, 2003
    Posts: 24183
        
      34

    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.


    [Jess in Action][AskingGoodQuestions]
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    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

    Joined: Jan 29, 2003
    Posts: 8791
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: comparing help!