Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

need some help (java.util.Date)  RSS feed

 
christopher persinger
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello all. could anyone please help me or give me some input about a small problem i have run in to. okay, im writing a program that will read in 3 different text files and have them sorted into array lists. well i am working on the 3 main class definitions and i have run into a problem. the first 2 classes i just created strings and then ran a equals and a compareTo on them to determine which entry would be first alphabetically when it is entered into the database. well on the last class.. i wanted to do the same thing (create 2 strings and compare them that way).. but i have a java.util.date variable that i am dealing with. is it okay to read in the date into a string like i have been doing? or do i need to do something else.. i am posting the code below so please have a look and get back to me. thanks in advance!

lastName (is a String)
firstName (is a String)
title (is a String)
dateOfPurchase (is a Date)

public boolean equals(Order anOrder)
{
boolean result;

String x1 = this.dateOfPurchase + this.lastName + this.firstName + this.title;
String x2 = anOrder.dateOfPurchase + anOrder.lastName + anOrder.firstName + anOrder.title;

int i = 0;
result = true;

while (i < x1.length())
if (x1.charAt(i) == x2.charAt(i))
i++;
else
result = false;

return result;
}
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, String overloads the equals method, so you don't need to implement this logic yourself. This means that you can reduce your own method to the following:

In my opinion, this is much easier to read. Note that Date also overloads the equals() method, so you don't even need to convert the Date to a String first. In other words, you can reduce this method to a single line of code:

Notice that I am just comparing each member field for equality here. Also, this is a much more general solution because it only relies on the fact that the classes involved override the equals() method themselves. In my mind, this makes more sense when you are implementing a custom equals() method than it does to rely on classes overriding toString(). On the other hand, if I were implementing a custom toString() method, I would rely on the toString() method from other classes.

I hope this helps. And welcome to the Ranch!

Layne

[ August 27, 2005: Message edited by: Layne Lund ]
[ August 27, 2005: Message edited by: Layne Lund ]
 
christopher persinger
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was told by someone in another forum that i need to make my equals method ... (Object anOrder) .. instead of (Order anOrder)... does this matter??

also, on the equals idea you just gave me.. i am needing to compare each individual letter in order to be able to place them in alphabetical order.. does the code you give me do that?

thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!