• 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

Reading a text file and creating two types of objects, but the first object type is created twice

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an object 'liman', liman contains ships and containers in ArrayLists. I'm trying to fill the ArrayLists from a text file which goes like this:

1574683 Abyssinia
8546279 Royal_Oak
2454438 Oceanway
9146578 Atilgan
3378841 Albatros
6562152 Imbat


1000085321 N N
1000255963 N N
1000511926 N N
1000853210 N N
1001279815 N N
1001791741 N N
1002388988 N N
...



This file has 6 ships, their IMOs and their names. It has 624 containers, their serial numbers and two features. Here's my method:



This code successfully creates containers and adds them to the liman's ArrayList for containers. It's the same for the ships as well, but it creates the ships twice. So liman's ArrayList for ships contains the same ship twice, the size of the arraylist is 12. What am I doing wrong? (by the way, I'm a rookie)
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

I created a test class with your method and it fills only 6 ships, so the problem is somewhere else. A couple of hints:

1) You're receiving an object as a parameter and altering the inner state of that object. That is usually a bad practice that leads to problems such as the one you ran into. You might try instead creating the Liman object inside the method and returning it.

2) If you print the lists before the method execution and after you might notice if something weird happened inside the method of ir the problem is outside.

Cheers.
reply
    Bookmark Topic Watch Topic
  • New Topic