title: Assigning properties to an object in Java from a text file
body: <p>I'm currently building a gamebook text based adventure program which will accept a certain format of txt file which looks like the one below.</p>
  
  <p>Now this may all seem pretty simple however, I need to create Section objects in order for my game to work properly. As you can see above, the sections are often different sizes depending on how many possible choices they have to their appropriate story text. My section object has the declaration of </p>
  
  <pre><code>Section(String storytext, String choiceText, int choiceSections)
  </code></pre>
  
  <p>And I need to pull the appropriate details from this text file. I was reading through some forums and found an somewhat appropriate looking approach to the reading through the text file. The code was as follows which works in triplets, not necessarily good for me as my text files come in variable lengths. And obviously this is just an example of code, I would need to parse out both integers and strings.</p>
  
  <pre><code>Car[] car = new Car[length/3];
  
  for (int i = 0; i &lt; length/3; i ++) // 
  {
      int startReading = Integer.parseInt(inputFile.readLine());
      int endReading = Integer.parseInt(inputFile.readLine());
      int liter = Integer.parseInt(inputFile.readLine());
      car[i] = new Car (startKm, endKm, litre);
  }
  </code></pre>
  
  <p>This seems like it would work for me however, I need my loop to update dynamically based upon a few criteria. #1 > The number on the first line which is the total number of section objects which will be created. #2 > The number on the total choices line which will determine how many more lines the line reader needs to run through before stopping and creating the object. I'm quite lost on how to create a proper loop which will create my objects based on this criteria for these text files.</p>
  
  <p>6 &lt; Number of "sections" in the game</p>
  
  <p>1 &lt; Section number (1)</p>
  
  <p>While making a peanut butter and jam sandwich one evening you discover that you’ve run out of strawberry jam. &lt; Story text associated with the section</p>
  
  <p>2 &lt; Number of possible choices</p>
  
  <p>5 &lt; The choice below leads to this section</p>
  
  <p>If you make do with grape jelly, click here. &lt; This is one of the choices</p>
  
  <p>4 &lt; The choice below leads to this section</p>
  
  <p>If you head out to the store to buy strawberry jam, click here. &lt; This is one of the choices</p>
  
  <p>2 &lt; Section number (2)</p>
  
  <p>You arrive home and begin making a fresh sandwich with your favourite condiment. You lovingly spread the peanut butter and the strawberry jam, salivating at the wonderful aroma. Unable to resist any longer, you take a bite. Utter bliss envelopes you, the perfect blend of flavours a cascade of awesomeness filling your consciousness. &lt; Story text associated with this section</p>
  
  <p>0 &lt; Number of possible choices</p>
  
  <p>3 &lt; Section Number (3)</p>
  
  <p>Your stomach immediately starts to hurt. Pain stabs at your abdomen and you double over, gasping for breath. The floor crashes up and darkness clouds your vision. &lt; Story text associated with this section</p>
  
  <p>0 &lt; Number of possible choices</p>
  
  <p>4 &lt; Section Number (4)</p>
  
  <p>You rush to the grocery store and look for your favourite brand of strawberry jam. You are surprised to find a new variety, fieldberry, on the shelf beside the familiar jar. &lt; Story text associated with this section</p>
  
  <p>2  &lt; Number of possible choices</p>
  
  <p>2 &lt; The choice below leads to this section</p>
  
  <p>If you buy the strawberry jam, click here. &lt; This is one of the choices</p>
  
  <p>6 &lt; The choice below leads to this section</p>
  
  <p>If you buy the new fieldberry jam, click here. &lt; This is one of the choices</p>
  
  <p>5 &lt; Section Number (5)</p>
  
  <p>You dig around in the fridge for a while and find the grape jelly you remembered was back there. It smells a bit funky, but you slather it onto your bread just the same, holding your breath a bit. &lt; Story text associated with this section</p>
  
  <p>2  &lt; Number of possible choices</p>
  
  <p>3 &lt; The choice below leads to this section</p>
  
  <p>If you eat the smelly sandwich, click here. &lt; This is one of the choices</p>
  
  <p>4 &lt; The choice below leads to this section</p>
  
  <p>If you toss it in the garbage and go out to buy jam, click here. &lt; This is one of the choices</p>
  
  <p>6 &lt; Section Number (6)</p>
  
  <p>You arrive home and begin making a fresh sandwich with your exciting new 
  condiment. You lovingly spread the peanut butter and the fieldberry jam, salivating at the wonderful aroma. Unable to resist any longer, you take a bite. &lt; Story text associated with this section</p>
  
  <p>1  &lt; Number of possible choices
   //do not know the choice here yet.</p>
  
  <p>3  &lt; Choice section you are led too, 3 represents winning so it finishes at this point.</p>