• 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

I'm stuck!

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Allan -
Throw us a little sample code, and I'm sure you'll get a lot of feedback!
-Bert
 
Allan Peak
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 253
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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() .
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic