• 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

Initialize Object from File

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm successfully reading from a file that has the following format:

firstname
lastname
age

Here's an example:

Johnny
Appleseed
23

This is done by wrapping FileReader within a BufferedReader. I either store these values in String variables or an array of type String.

I also have Person class:


I would like to create a Person object based on the file contents. Here's what I've tried so far:



Instead of the array option, i've also tried:


Both options do work, but seems very "un-elegant". I don't know whether the file contents -> array option is okay. Is there a better option, should I use serialization or another data structure?
 
Marshal
Posts: 79039
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you should consider how you are reading from the file, and how you are creating objects.

Using a plain simple buffered reader has become a bit low‑level since the development of Scanner. If you go through the documentation for Scanner, there is an example about reading longs from a text file. You can expand that example with myScanner.next() and myScanner.nextInt() (and another hasNextXXX method) so you can get the two names and age from the file. Then you can pass them to the constructor. That is one way to do it. You will doubtless have realised there are lots of other ways to do it.

If you create a Person(String[]) constructor, that is brittle. What if the array doesn't have exactly 3 elements? What if the 3rd element (index=2) isn't an int? All sorts of things could go wrong.

What are you doing about files which have multiple lines in? If you use the next method of a Scanner that permits you to have your details on the same or on different lines.
 
Campbell Ritchie
Marshal
Posts: 79039
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not think serialisation will help you.
 
Johnny Venter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell for the Scanner hint. I got it working and it seems cleaner now.

Here is my updated Person class constructor:



Here is my implementation code:



I need to add some checking on the file contents, but this is better than the array option.
 
Campbell Ritchie
Marshal
Posts: 79039
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless you have reason to believe the names will be several words, on the same line, consider using next() rather than nextLine(). That of course depends on the names being one “token” each.
The reason is that there are problems with nextLine after nextAnythingElse. All will be well until you try to create a second Person object from the same text file. If things go wrong, never fear. I know the solution.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic