Java utilizing ArrayList to write and read contacts(object) to file with GUI -
i'm writing pretty simple program gui program emulates cell phone. "cell phone" has 4 main buttons: phone, contacts, message, , apps. i've coded gui , hit snag while working on contact class, backbone of entire program!
the contact class straightforward, has 2 instance variables of type string 'name' , 'number'. want build arraylist of type contact, allow contacts added, , create methods append , read in serialized file.
at point i'm stuck on how create methods add objects arraylist, , create methods append , read in serialized file.
here contact class:
public class contact { public string name, number; contact() {} contact (string thename, string thenumber) { this.name = thename; this.number = thenumber; } public void setname(string aname) { this.name = aname; } public void setnumber(string anumber) { this.number =anumber; } public string getname() { return name; } public string getnumber() { return number; } public string tostring() { return name + ": " + number; } public boolean equals(contact other) { if (name.equals(other.getname()) && number.equals(other.getnumber())) { return( true ); } else { return( false ); } } } thanks quick responses. i've corrected equals method , moved arraylist it's own class. cleared error on read method (java 7 issue). current issue i'm running on these lines:
out.writeobject(contact); and
contact contact = (contact)in.readobject(); since i'm trying write , read arraylist, shouldn't methods reflect that?
import java.util.*; import java.io.*; class contactscollection implements serializable { public static final long serialversionuid = 42l; arraylist <contact> contactlist = new arraylist<contact>(); public void write() { try { objectoutputstream out = new objectoutputstream(new fileoutputstream("contactlist.dat")); out.writeobject(contact); } catch(ioexception e) { e.printstacktrace(); } } public void read() { try { objectinputstream in = new objectinputstream(new fileinputstream("contactlist.dat")); contact contact = (contact)in.readobject(); } catch (ioexception e) { e.printstacktrace(); } catch (classnotfoundexception e) { e.printstacktrace(); } } }
i afraid understand trying achieve can see 2 problems in code:
- you have created contact class static attributes, means objects share same value of name & number. seems problematic , advice create non-static attributes when there seems no need share them.
public static string name, number;
second problem in class have put arraylist of contacts in it. , have made class seriablizable. means everytime serialize contact object, arraylist serialized along it, assume not desired. should create class , name such contactscollection , create arraylist in it, may chose have static arraylist here.
in gui, if creating , object, create instance of contact. , add contactscollection.contactlist(). may add methods serialize in contactscolleciton class. may chose have single method in contactscollection argument contact object. first add object in list , serialize it.
Comments
Post a Comment