Results 1 to 2 of 2
  1. #1
    kalimm is offline Member
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Default Program to print average rainfall

    This program will compute and print the average rainfall per month by year and the overall average rainfall per month. There will be exactly 12 values of input for each year entered and may be any number of years.

    My problem is that my program doesn't seem to be exiting the "while" loop that is in my main method. When I enter rainfall values for 3 years, my output only shows the first 2. If I want to show the lines that are printed after the while loop, I have to enter something that will make the program error out. Any ideas of what might be causing this?

    Java Code:
    Java Code:
    import java.util.Scanner;
    import java.text.DecimalFormat;
    public class Rainfall
    {
       public static void main(String[] args)
       {
          // Declare needed constants
    
          final boolean DEBUG_SW = true;
    
          final int VALUES_PER_LINE = 12;
    
          // Declare variables
    
          Scanner scan = new Scanner(System.in) ;
    
          DecimalFormat fmt = new DecimalFormat("0.00");
    
          int year = 1;
    
          double
             yearTotal = 0.0,
             grandTotal = 0.0,
             overallAveragePerMonth = 0.0;
    
          // Initialize outer loop variables as necessary.
          double averagePerMonth = 0.0;
          double averagePerYear = 0.0;
    
          // Outer loop:
    
          while(scan.hasNextDouble())
          {
    
             // Call second method:
    
             yearTotal = yearScan(scan, VALUES_PER_LINE, DEBUG_SW, fmt);
    
             // Accumulate a running grand total.
             grandTotal += yearTotal;
    
    // Print debug information as necessary.
             if(DEBUG_SW)
             {
                System.out.println("DEBUG   Grand total of rainfall so far is : "
                   + fmt.format(grandTotal) + "\n");
             }
    
             // Calculate and print out any necessary values.
             averagePerMonth = yearTotal / VALUES_PER_LINE;
    
             System.out.println("\nAmount of rainfall for year " + year  +
                " was " + fmt.format(yearTotal));
             System.out.println("Average rainfall per month was " +
                fmt.format(averagePerMonth) + "\n");
    
             // increment the number of years
             year++;
          } // end of outer loop
    
          // Compute and print final values as needed.
          averagePerYear = grandTotal / year;
          overallAveragePerMonth = grandTotal / (VALUES_PER_LINE * year);
    
          System.out.println("Grand total of rainfall for all years was " +
             fmt.format(grandTotal));
          System.out.println("Average rainfall per year was " +
             fmt.format(averagePerYear));
          System.out.println("Average rainfall per month for all years was " +
             fmt.format(overallAveragePerMonth));
    
       } // End of main method
    
    
       public static double yearScan(
          Scanner scan, int numValues, boolean debugSw, DecimalFormat fmt)
       {
          // Declare local method variables:
          double
             value = 0.0,
             yearTotal = 0.0,
             grandTotal = 0.0;
          int year = 1;
    
    // Loop to read in rainfall values for each month:
    
          for(int i = 0; i < numValues; i++)
          {
             // Check that there is a "double" value to be read,
             //    read it, and check that it is not negative.
             // On any bad data, print an error message and terminate.
             if(scan.hasNextDouble())
             {
                value = scan.nextDouble();
             }
             else
             {
                System.out.println("Error: Input must be a double.");
                System.out.println("Quitting program.");
                System.exit(0);
             }
             if(value <= 0)
             {
                System.out.println("Error: Input must be positive.");
                System.out.println("Quitting program.");
                System.exit(0);
             }
    
             // Accumulate a running sum of the rainfall for this year.
             yearTotal += value;
    
             // Print debug information as necessary.
             if(debugSw)
             {
                System.out.println("DEBUG   Amount of rainfall so far this " +
                   "year is " + fmt.format(yearTotal));
             }
          }
    
          // Return the total rainfall for the year.
          return yearTotal;
    
       } // End of yearScan method
    
    } // End of Class
    Last edited by Fubarable; 07-07-2012 at 07:19 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 2
    Last Post: 08-01-2011, 12:19 AM
  2. A java program to print short form of a name.
    By Neeer in forum New To Java
    Replies: 2
    Last Post: 03-20-2011, 07:16 AM
  3. Replies: 4
    Last Post: 02-16-2011, 12:05 AM
  4. Replies: 10
    Last Post: 12-20-2010, 01:47 PM
  5. sum,product,average program
    By chitwood in forum New To Java
    Replies: 7
    Last Post: 01-24-2008, 06:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •