title: Handling blank lines with txt input file and Scanner
body: <p>I have my program complete. The one thing I need is a way for it to handle blank lines. I have read other posts but none have helped me implement them into my own code. I've tried various syntax within the loop that reads the file, but none have worked. Can someone help me please. This seems like it should be easier than it feels like currently. I'm trying to implement this into my code but am having trouble doing it. Below are my two classes and the input.txt. As is, this program runs as intended. Thanks for any help.</p>
  
  <pre><code>String line = in.nextLine();
  while (line.length() == 0) {
    if (in.hasNext()) {
       line = in.nextLine();
    } else {
      break;
    }
  }
  if (line.length() == 0) {
     // reaches the end of input file
  }
  </code></pre>
  
  <p>Product.java</p>
  
  <pre><code>/**
   * Product
   * 
   *   A simple class framework used to demonstrate the design
   *   of Java classes.
   *   
   *   @author 
   *   @version 02042015
   */
  import java.util.*;
  
  public class Product {
      private String name;
      private String code;
      private int quantity;
      private double price;
      private String type;
      private ArrayList&lt;Integer&gt; userRatings;
  
  
      /*
       * Product constructor
       */
      public Product() {
          name = "";
          code = "";
          quantity = 0;
          price = 0.0;
          type = "";
          userRatings = new ArrayList&lt;Integer&gt;();
  
      }
       public Product(Product productObject) {
              this.name = productObject.getName();
              this.code = productObject.getInventoryCode();
              this.quantity = productObject.getQuantity();
              this.price = productObject.getPrice();
              this.type = productObject.getType();
              this.userRatings = new ArrayList&lt;Integer&gt;();
          }
  
      public Product(String name, String code, int quantity, double price, String type) {
          this.name = name;
          this.code = code;
          this.quantity = quantity;
          this.price = price;
          this.type = type;
          this.userRatings = new ArrayList&lt;Integer&gt;();
      }
      /*
       * setName
       *  @param name - new name for the product
       */
      public void setName(String name) {
          this.name = name;
      }
  
      /*
       * getName
       *  @return the name of the product
       */
      public String getName() {
          return name;
      }
  
      /*
       * setType
       *  @param type - the type of the product
       */
      public void setType(String type) {
          this.type = type;
      }
  
      /*
       * getType
       * @return - the product type
       */
      public String getType() {
          return type;
      }
  
      /*
       * setPrice
       * @param price - the price of the product
       */
      public void setPrice(double price) {
          this.price = price;
      }
  
      /*
       * getPrice
       * @return the price of the product
       */
      public double getPrice() {
          return price;
      }
  
      /*
       * setQuantity
       * @param quantity - the number of this product in inventory
       */
      public void setQuantity(int quantity) {
          this.quantity = quantity;
      }
  
      /*
       * getQuantity
       * @return the number of this product in inventory
       */
      public int getQuantity() {
          return quantity;
      }
  
      /*
       * setInventoryCode
       * @param code - the new inventory code for the product
       */
      public void setInventoryCode(String code) {
          if(code.length()!= 8){
              System.out.println("An invalid code has been entered. Please enter a code that is 8 characters in length.");
          }
          else{
          }
          this.code=code;
      }
  
      /*
       * getInventoryCode
       * @return the inventory code of the product
       */
      public String getInventoryCode() {
          return code;
      }
  
      /*
       * setRatings
       * @param code the new set of ratings for the product
       */
      public void setRatings(ArrayList&lt;Integer&gt; Ratings){
          this.userRatings = Ratings;
      }
  
      /*
       * getRatings
       * @return the ratings of the product
       */
      public ArrayList&lt;Integer&gt; getRatings(){
          return userRatings;
      }
  
      /*
       * addUserRating
       * NOTE: Each individual rating is stored with the product, so you need to maintain a list
       * of user ratings.  This method should append a new rating to the end of that list
       * @param rating - the new rating to add to this product
       */
      public void addUserRating(Integer rating1) {
          if(rating1 &gt; 5 || rating1 &lt; 0){
              System.out.println("You have entered an invalid rating. Please enter a rating between one and five stars.");
          }
          this.userRatings.add(rating1);
      }
  
  
      /*
       * getUserRating
       *  NOTE:  See note on addUserRating above.  This method should be written to allow you
       *  to access an individual value from the list of user ratings 
       * @param index - the index of the rating we want to see
       * @return the rating indexed by the value index
       */
      public int getUserRating(int index) {
          int a = this.userRatings.get(index);
          return a;
      }
  
      /*
       * getUserRatingCount
       *  NOTE: See note on addUserRating above.  This method should be written to return
       *  the total number of ratings this product has associated with it
       * @return the number of ratings associated with this product
       */
      public int getUserRatingCount() {
          int a = this.userRatings.size();
          return a;
      }
  
      /*
       * getAvgUserRating
       *  NOTE: see note on addUserRating above.  This method should be written to compute
       *  the average user rating on demand from a stored list of ratings.
       * @return the average rating for this product as a whole integer value (use integer math)
       */
      public String getAvgUserRating() {
           int sum = 0;
            String avgRating = "";
           if (userRatings.size() != 0){
            for (int i = 0; i &lt; this.userRatings.size(); i++) {
                int a = getUserRating(i);
                  sum += a;
            }
            double avg = sum/this.userRatings.size();
            if(avg &gt;= 3.5){
                avgRating = "****";
            }
            else if(avg &gt;= 2.5){
                avgRating = "***";
            }
            else if(avg &gt;= 1.5){
                avgRating = "**";
            }
            else if(avg &gt;= 0.5){
                avgRating = "*";
            }
            else{
            }
           }
           else{
               avgRating = "";
           }
            return avgRating;
  }
  }
  </code></pre>
  
  <p>Project02.java</p>
  
  <pre><code>/**
   *  Inventory Reporting Program
   * 
   *  A simple set of methods used to report and summarize
   *  the information read from an inventory file of product data
   *  
   *   @author 
   *   @version 02042015
   */
  
  import java.util.*;
  import java.io.*;
  
  public class Project02 {
  
      public static void main(String[] args) {
          //Establish the scanner so user input can be properly read.
          Scanner keyboard = new Scanner(System.in); 
                  System.out.print("Enter an inventory filename: ");
                  String fname = keyboard.nextLine();
                  ArrayList&lt;Product&gt; products = loadProducts(fname);
                  generateSummaryReport(products);
                  highestAvgRating(products);
                  lowestAvgRating(products);
                  largestTotalDollarAmount(products);
                  smallestTotalDollarAmount(products);
      }
  
      public static void generateSummaryReport (ArrayList&lt;Product&gt; Products){
          int counter = 0;
          System.out.println("Product Inventory Summary Report");
          System.out.println("----------------------------------------------------------------------------------");
          System.out.println();
          System.out.printf("%-33s%-10s%-6s%-7s%-7s%-7s%-7s", "Product Name", "I Code", "Type", "Rating", "# Rat.", "Quant.", "Price");
          System.out.println();
          System.out.printf("%-33s%-10s%-6s%-7s%-7s%-7s%-7s", "-------------------------------", "---------", "----", "------", "------", "------", "------");
          System.out.println();
      while(counter &lt; Products.size()){
          System.out.printf("%-33s%-10s%-6s%-7s%6s%7s%7s", Products.get(counter).getName(), Products.get(counter).getInventoryCode(), Products.get(counter).getType(), Products.get(counter).getAvgUserRating(), Products.get(counter).getUserRatingCount(), Products.get(counter).getQuantity(), Products.get(counter).getPrice());
          System.out.println();
          counter++;
      }
      System.out.println("----------------------------------------------------------------------------------");
      System.out.println("Total products in the database: " + Products.size());
  }
  
  
      /*
       * loadProducts
       *  Given a filename, opens the file and reads Products from
       *  the file into an ArrayList of Product objects.  Returns
       *  the ArrayList.
       *  
       *  
       *  @param fname - String containing the input file name
       *  @return - An ArrayList of Product objects
       */
      public static ArrayList&lt;Product&gt; loadProducts(String fname) {
          int a = 0;
          Integer b = 0;
          ArrayList&lt;Product&gt; products = new ArrayList&lt;Product&gt;();
          try {
          Scanner inFile = new Scanner(new File(fname));
          while (inFile.hasNext()) {
              int counter = 0;
              String name = inFile.nextLine();
              String code = inFile.nextLine();
              int quantity = inFile.nextInt();
              double price = inFile.nextDouble();
              String type = inFile.next();
              Product productObject = new Product(name, code, quantity, price, type);
              while(inFile.hasNextInt() &amp;&amp; counter==0){
                  a = inFile.nextInt();
                  if(a != -1){
                  b = new Integer(a);
                  productObject.addUserRating(b);
                  }
                  else{
                  counter = 1;
                  }
              }
              products.add(productObject);
              if(inFile.hasNext()){
              inFile.nextLine();
          }
          }
          inFile.close();
      }
          catch (FileNotFoundException e) {
              System.out.println("ERROR: " + e);
          }
          return products;
  }   
      //Finds the item with the highest average user rating in stock
      public static void highestAvgRating(ArrayList&lt;Product&gt; Products){
          int counter = 0;
          int a = 1;
          while (counter &lt;= Products.size()-1){
              if(Products.get(counter).getAvgUserRating().length() &gt; Products.get(a).getAvgUserRating().length()){
                  a = counter;
              }
              else{
              }
              counter++;
          }
              System.out.println("Highest Average User Rating In Stock: " + Products.get(a).getName() + " ("+Products.get(a).getAvgUserRating() + ")");   
      }
  
      //Finds the item with the lowest average user rating in stock
      public static void lowestAvgRating(ArrayList&lt;Product&gt; Products){
          int counter = 0;
          int a = 1;
          while (counter &lt;= Products.size()-1){
              if(Products.get(counter).getAvgUserRating().length()&lt;Products.get(a).getAvgUserRating().length()){
                  a=counter;
              }
              else{
              }
              counter++;
          }
              System.out.println("Lowest Average User Rating In Stock: "+Products.get(a).getName() + " ("+Products.get(a).getAvgUserRating() + ")");  
      }
  
      //Finds the item with the largest total dollar amount in inventory (quantity * price)
      public static void largestTotalDollarAmount(ArrayList&lt;Product&gt; Products){
          int counter = 0;
          int a = 1;
          while (counter &lt;= Products.size()-1){
              if((Products.get(counter).getPrice())*(Products.get(counter).getQuantity()) &gt; ((Products.get(a).getPrice())*(Products.get(a).getQuantity()))){
                  a=counter;
              }
              else{
              }
              counter++;
          }
              System.out.println("Item With The Largest Total Dollar Amount In Inventory: " + Products.get(a).getName() + " ($" + ((Products.get(a).getPrice())*(Products.get(a).getQuantity())) + ")");  
      }
  
      //Finds the item with the smallest total dollar amount in inventory (quantity * price)
      public static void smallestTotalDollarAmount(ArrayList&lt;Product&gt; Products){
          int counter = 0;
          int a = 1;
          while (counter &lt;= Products.size()-1){
              if((Products.get(counter).getPrice())*(Products.get(counter).getQuantity()) &lt; ((Products.get(a).getPrice())*(Products.get(a).getQuantity()))){
                  a=counter;
              }
              else{
              }
              counter++;
          }
              System.out.println("Item With The Smallest Total Dollar Amount In Inventory: " + Products.get(a).getName() + " ($" + ((Products.get(a).getPrice())*(Products.get(a).getQuantity())) + ")"); 
      }
  }
  </code></pre>
  
  <p>input.txt</p>
  
  <pre><code>The Shawshank Redemption
  C0000001
  100
  19.95
  DVD
  4
  5
  3
  1
  -1
  The Dark Knight
  C0000003
  50
  19.95
  DVD
  5
  2
  3
  -1
  Casablanca
  C0000007
  137
  9.95
  DVD
  5
  4
  5
  3
  -1
  The Girl With The Dragon Tattoo
  C0000015
  150
  14.95
  Book
  4
  4
  2
  -1
  Vertigo
  C0000023
  55
  9.95
  DVD
  5
  5
  3
  5
  2
  4
  -1
  A Game of Thrones
  C0000019
  100
  8.95
  Book
  -1
  </code></pre>