Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes I'm stuck! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I Watch "I New topic
Author

I'm stuck!

Allan Peak
Greenhorn

Joined: Jul 10, 2002
Posts: 7
I'm trying to write a Java program for my own use. However, I'm stuck on two things.
1. How do I read/write my user-defined classes to a file?
2. Can I sort my user-defined class on one or more key fields?
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Allan -
Throw us a little sample code, and I'm sure you'll get a lot of feedback!
-Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Allan Peak
Greenhorn

Joined: Jul 10, 2002
Posts: 7
I'm mostly in the planning stages, and don't have much useable code yet, but here's what my class currently looks like:
public class Check
{
String cYear;
String cMonth;
String cDay;
String cNum;
String payee;
String memo;
String category;
Boolean balanced;
float amt;
}
I'd like to sort on the date fields & the cNum field.
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
To write an object, you can use writeObject( ), which is a member of ObjectOutputStream. To read an object, you can use readObject(), which is a member of ObjectInputStream. However, these may not be applicable to what you are actually trying to do.


For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, Allan -
Just a few quick general pointers:
1. You wouldn't sort a class. You would build a collection of the class's instances, then sort the collection. It would be up to you to define how the comparison for sorting is done, by overriding the equals() method.
2. Look into "serializable".
3. Look into the File class and the output writers/streams. (Writers are for text-based data, streams for binary data.)
Sun has online tutorials that help a lot with learning the Java technology. (I posted these general statements because I don't know what level you're at in Java, whether beginner or more advanced.)
Hope that helps!


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34


1. You wouldn't sort a class. You would build a collection of the class's instances, then sort the collection. It would be up to you to define how the comparison for sorting is done, by overriding the equals() method.

The equals() method isn't going to help with sorting. Instead, look at the Comparable and Comparator interfaces, and at the static method java.util.Arrays.sort() .


[Jess in Action][AskingGoodQuestions]
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
I stand (well, sit) corrected on equals(). I think my brain was back at old C comparison library functions, which return a negative value, 0, or a positive value, depending on the comparison done.
My apologies for the confusion!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I'm stuck!