• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

I'm stuck!

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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: 8998
19
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 805
  • Mark post as helpful
  • send pies
  • 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: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • 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: 805
  • Mark post as helpful
  • send pies
  • 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!
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!