This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I attempted to insert a data into an ArrayList and write it into a file and then later on pull it out then read and print it out. However, each time I run the program, when I insert the data using scanner class, instead of adding it into the list, it rewrites the entire file instead, displaying only the inserted input. I don't understand what went wrong, as it is supposed to update phoneList and then called it out as another list called newPhoneList and print, but it now only prints whatever is provided in the input. The constructor is as below:
There's your List - a newly created one. You then add one single element to it, and write it to the file. Of course this file will have a List with only have one element.
What you need to do is read the List from the file instead of creating it each time again. Only if the file does not exist should you create a new List:
That's what you're doing in your main method:
1) Let the user enter a 'phone' and print it out
2) Serialize a list of 'phone' objects that always contains only one element
3) Recreate a list of 'phone' objects that always contains only one element from serialized file and print it out
That's what you probably want to do:
1) Read previously serialized contents
2) Let user enter new 'phone' object
3) Serialize the list
Don't work with 'phoneList' and 'newPhoneList' ... but always with the same list.
Censorship is the younger of two shameful sisters, the older one bears the name inquisition. -- Johann Nepomuk Nestroy
Hi Rob, I can see now what the logical error is. Instead of only creating a new list for the first time, I've repeatedly create a list, replacing the old one each time I run the program. That would explain why it is only displaying one input and not adding on to the list.
Hi,Peter, what you're implying is that, when I insert data into a list, I should also read from the same list? Doesn't that mean that the 2nd part of the code where the newPhoneList takes place is redundant? I've always had the impression that when I perform Serialization, after I insert the data into a prior list, i have to create another new list to read from the first.
wuan luen hong
Joined: Aug 23, 2010
Hi again Rob,
Regarding your reply earlier, if the file should exist, the logic would be to immediately grab the list in the existing file instead of creating a new list. I don't however know how to read it as a list again. Is it done using the readObject() method?
I tinkered with the code a little and came out with this:
Although now I can input and keep the first information there, I still can't append the next few information into that text file (phoneFile.txt) that I'll be entering in the next few attempts. It gives me an error when i use the method writeObject() (in the commented part towards the end of the code) to update the text file, saying incompatible type or something. Any idea?
But you are now duplicating much code. Think the following through: what is the only difference between having an existing file and having to create a new List? Is the entire reading of user input part of it? Is the writing of the List at the end part of it?
You should separate your code into three parts:
- the creation / reading of the list
- handling user input
- writing the results back to the file