 <p>You have a number of issues you need to overcome...</p> <ul> <li>For each line, you need to determine the type of data the line represents</li> <li>You need some way to keep track of the data you've loaded (of the clients and their pets)</li> <li>You need some way to associate each pet with it's owner</li> </ul> <p>The first could be done in a number of ways, assuming we can change the data. You could make the first token meaningful (<code>human</code>, <code>pet</code>); you could use JSON or XML instead. But lets assume for the moment, you can't change the format.</p> <p>The key difference between the two types of data is the number of tokens they contain, 7 for people, 5 for pets.</p> <pre><code>while (input.hasNext()) { String text = input.nextLine(); String[] parts = text.split(","); if (parts.length == 7) { // Parse owner } else if (parts.length == 5) { // Parse pet } // else invalid data </code></pre> <p>For the second problem you could use arrays, but you would need to know in advance the number of elements you will need, the number of people and for each person, the number of pets</p> <p>Oddly enough, I just noticed that the last element is an <code>int</code> and seems to represent the number of pets!!</p> <pre><code>Morely,Robert,123 Anywhere Street,15396,4,234.56,2 ------------^ </code></pre> <p>But that doesn't help us for the owners.</p> <p>For the owners, you could use a <code>List</code> of some kind and when ever you create a new <code>Human</code>, you would simply add them to the <code>List</code>, for example...</p> <pre><code>List&lt;Human&gt; humans = new ArrayList&lt;&gt;(25); //... if (parts.length == 7) { // Parse the properties human = new Human(...); humans.add(human); } else if (parts.length == 5) { </code></pre> <p>Thirdly, for the pets, each <code>Pet</code> should associated directly with the owner, for example:</p> <pre><code>Human human = null; while (input.hasNext()) { String text = input.nextLine(); String[] parts = text.split(","); if (parts.length == 7) { //... } else if (parts.length == 5) { if (human != null) { // Parse pet properties Pet pet = new Pet(name, type, age, date1, date2); human.add(pet); } else { throw new NullPointerException("Found pet without human"); } } </code></pre> <p>Okay, so all this does, is each time we create a <code>Human</code>, we keep a reference to the "current" or "last" owner created. For each "pet" line we parse, we add it to the owner.</p> <p>Now, the <code>Human</code> class could use either a array or <code>List</code> to manage the pets, either will work, as we know the expected number of pets. You would then provide getters in the <code>Human</code> class to get a reference to the pets.</p> <p>Because out-of-context code can be hard to read, this is an example of what you might be able to do...</p> <pre><code>Scanner input = new Scanner(new File("data.txt")); List&lt;Human&gt; humans = new ArrayList&lt;&gt;(25); Human human = null; while (input.hasNext()) { String text = input.nextLine(); String[] parts = text.split(","); if (parts.length == 7) { String firstName = parts[0]; String lastName = parts[1]; String address = parts[2]; int cid = Integer.parseInt(parts[3]); int vists = Integer.parseInt(parts[4]); double balance = Double.parseDouble(parts[5]); int other = Integer.parseInt(parts[6]); human = new Human(firstName, lastName, address, cid, vists, balance, other); humans.add(human); } else if (parts.length == 5) { if (human != null) { String name = parts[0]; String type = parts[1]; int age = Integer.parseInt(parts[2]); String date1 = parts[3]; String date2 = parts[4]; Pet pet = new Pet(name, type, age, date1, date2); human.add(pet); } else { throw new NullPointerException("Found pet without human"); } } } </code></pre>