• 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

IO / Serialization

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:


And this part is the main method:

 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

wuan luen hong wrote:


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:
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is because everytime you are creating a new file..not checking for existing file and data in it..
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
beaten 3 minutes by Peter Taucher & Rob Prime
 
wuan luen hong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're already doing it, with newPhoneList.
 
wuan luen hong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not without seeing the error.

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic