title: Program output is NaN instead of float
body: <p>I am working on an exercise from a book, the exercise sounds like this:</p>
  
  <p>Drivers are concerned with the mileage their automobiles get. One driver has kept track of several trips by recording the miles driven and gallons used for each tankful. Develop a Java application that will input the miles driven and gallons used (both as integers) for each trip. The program should calculate and display the miles per gallon obtained for each trip and print the combined miles per gallon obtained for all trips up to this point. All averaging calculations should produce floating-point results. Use class Scanner and sentinel-controlled repetition to obtain the data from the user.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import java.util.Scanner;
  
  public class consumption {
      public static void main(String[] args) {
          Scanner in = new Scanner(System.in);
          int miles = 0;
          int gallons = 0;
          int totalGallons = 0;
          int totalMiles = 0;
          float mpg = 0;
          float totalAverage = 0;
  
          System.out.println("Enter number of gallons or enter -1 to finish:");
          gallons = in.nextInt();
          while(gallons != -1)
          {
              gallons += totalGallons;
              System.out.println("Enter the number of miles driven:");
              miles = in.nextInt();
              miles += totalMiles;
              mpg = ((float)totalMiles/totalGallons);
              System.out.printf("Total Miles per Gallon on this trip is %.2f\n", mpg);
              System.out.println("Enter number of gallons:");
              gallons = in.nextInt();
          }
  
          if(totalGallons!=0)
          {
              totalAverage = (float) totalMiles/totalGallons;
              System.out.printf("Total consumption on all trips is %.2f\n", totalAverage);
          }
          else
              System.out.println("You did not enter a valid gallon quantity\n");
      }
  }
  </code></pre>
  
  <p>For some reason, after I enter the sentinel (-1), the output shows NaN instead of the float number it should output.</p>
  
  <p>Also, it does not calculate the <code>totalAverage</code>, not even showing NaN </p>
  
  <p>This is the output:</p>
  
  <p>Enter number of gallons or enter -1 to finish: 25</p>
  
  <p>Enter the number of miles driven: 5</p>
  
  <p>Total Miles per Gallon on this trip is NaN</p>
  
  <p>Enter number of gallons: -1</p>
  
  <p>You did not enter a valid gallon quantity</p>
  
  <p>Process finished with exit code 0</p>
  
  <p>Please help me :(</p>