title: How to read multiple data type file into ArrayList using Scanner
body: <p>I am trying to read a file of multiple data types into an ArrayList object using Scanner with delimiter "\s\s", however it doesn't seem to work as intended. I am using printf to view if data would be stored correctly, data which I will use for calculations later on. The data seem to display correctly however I am still getting an "Incorrect File Format" exception. Also there seems to be a problem with loop. I always get stuck when using ArrayList of objects.</p>
  
  <p>Sample text file:</p>
  
  <pre><code>item  item descr  100  1.50
  item2  item descr  250  2.50
  item2  item descr  250  3.50
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  
  public class ReadItems
  {
  
      private Scanner input;
      ArrayList&lt;Item&gt; item = new ArrayList&lt;Item&gt;();
  
      //open text file
      public void openFile()
      {
          try
          {
              FileReader in = new FileReader("Items.txt");
              input = new Scanner(in).useDelimiter("\\s\\s");
          }
          catch( FileNotFoundException fileNotFound)
          {
              System.err.println( "Error opening file.");
              System.exit(1);
          }
      }
  
      //read file
      public void readFile()
      {
          try
          {
              while ( input.hasNextLine())
              {
                  item.add( new Item(input.next(), input.next(), input.nextInt(), input.nextFloat() ));                                       
                  for (Item list : item) 
                  {
                      System.out.printf("%-10s%-48s$%5.2f\n", list.getCode(), (list.getDecription()+ ", "+ list.getWeight()+ "g"), + list.getPrice());
                      //System.out.println(item);
                  }
  
              }
  
          }
          catch ( NoSuchElementException elementEx)
          {
              System.err.println( "Incorrect file format.");
              System.exit(1);
          }
          catch ( IllegalStateException stateEx )
          {
              System.err.println( "Error reading from file.");
              System.exit(1);
          }
  
      }
  
      public void closeFile()
      {
          if (input != null)
              input.close();      
      }
  
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>item      item descr, 100g                                $ 1.50
  item      item descr, 100g                                $ 1.50
  item2     item descr, 250g                                $ 2.50
  item      item descr, 100g                                $ 1.50
  item2     item descr, 250g                                $ 2.50
  item2     item descr, 250g                                $ 3.50
  </code></pre>
  
  <p><b>Incorrect file format.</b></p>
  
  <p>Sorry it seems i was doing a dumb thing. I wasn't running the program through my test class where <code>main</code> is. </p>
  
  <p>Test Class:</p>
  
  <pre><code>public class TestReadItems
  {
  
  public static void main(String[] args) 
  {
  ReadItems application = new ReadItems();
  application.openFile();
  application.readFile();
  application.closeFile();
  }
  }
  </code></pre>
  
  <p>The program runs without errors however i can't seem to get the while loop to work properly. Output is tripled.</p>