title: Using Scanner in Java to parse a text file or console input and ran into errors
body: <p>I am trying to read in values from a file separated by white space between the individual items and carriage returns.</p>
  
  <p>So the text file is something like</p>
  
  <pre><code>1 lunch box 3.99
  2 jelly donuts 1.99
  4 cases of beer 36.99
  2 chess boards 24.99
  </code></pre>
  
  <p>With these values I am trying to construct objects... So the object will be made up of the quantity, the first thing on the line, a name - the second thing on the line, and a price the last thing on the line. So the constructor is like:</p>
  
  <pre><code>product(int x, String name, float price) { 
   this.x = x;
   this.name = name;
   this.price = price;
  }
  </code></pre>
  
  <p>My problem is the way I have coded the scanner is running into trouble when trying to parse everything. So I receive a java.util.InputMismatchException. If I use an algorithm like:</p>
  
  <pre><code>if (scannerRef.hasNextInt()) 
    arraylistRef.intThing = scannerRef.nextInt();
  
  if (scannerRef.hasNext())
     arraylistRef.name = scannerRef.next();
  
  if (scannerRef.hasNextFloat())
     arrayListRef.price = scannerRef.nextFloat();
  </code></pre>
  
  <p>then the code the way I have it puts each individual primitive or String in its own object instead of looping through an entire line then adding it to an object</p>
  
  <p>I hope this makes sense. Thanks for reading if you are reading this now.</p>
  
  <p>the main part of my code I am concerned about is this: 
  the way I have it here with the above type of algorithms commented out puts everything in to the same object but runs into the java.util.InputMismatchException dingo brains</p>
  
  <pre><code>// loop while there is text to read
  while (s1.hasNextLine()) {
      Product p = new Product(p.quantity = s1.next);
    //if (s1.hasNextInt())
       p.quantity = s1.nextInt();
  
  
  //  else 
       p.name = s1.next(); // to get strings
  
       // else if(s1.hasNextFloat())
       p.price = s1.nextFloat();
  
   // now add the line to the product arraylist
   foo.add(p);
  
  }
  </code></pre>