# Average Rainfall Main Class Using nested for loops,input validation - Average is off

• February 23rd, 2014, 01:42 PM
CyberOps
Average Rainfall Main Class Using nested for loops,input validation - Average is off
Hello - I follow posts on this forum and this is my first posting, thank you.

When input validation for the first months rainfall is non-negative, this results in correct average rainfall.
When input validation is used for the first months rainfall I'm prompted to input a positive number, which is 2.
When asked to input rainfall, in inches, for each month, I begin with input -3, I am again prompted to re-enter
a positive, I enter 3. What happens is, whichever positive integer I input after I had entered a negative for
the first months rainfall, the average would be off by the positive number inputted.

Code Java:

```package averagerainfall; import java.util.Scanner;   public class AverageRainfall { public static void main(String[] args) { int maxYears; int totalMonths; int monthCount = 0; double rainInches = 0.0; double totalInches = 0.0; // Initialize total inches or rain double averageRainInches = 0.0; // Initialize total average of rain   Scanner keyboard = new Scanner(System.in);   System.out.println("Enter the number of years: "); maxYears = keyboard.nextInt();   while (maxYears < 1) { System.out.println("Invalid. Enter 1 or greater:"); maxYears = keyboard.nextInt(); } totalMonths = maxYears * 12; System.out.print("Enter the rainfall, in inches, for each month.\n");   for (int yearCount = 1; yearCount <= maxYears; yearCount++) { for (monthCount = 1; monthCount <= 12; monthCount++) { System.out.println("Year " + yearCount + " month " + monthCount + ":"); rainInches = keyboard.nextInt();   while ( rainInches < 0 ) { System.out.println("Invalid. Enter 0 or greater:"); rainInches = keyboard.nextInt(); totalInches += rainInches; } totalInches += rainInches; } } averageRainInches = (double) totalInches / totalMonths;   System.out.println("Number of months: " + totalMonths); System.out.println("Total rainfall: " + totalInches); System.out.println("Average monthly rainfall: " + averageRainInches + " inches"); }   }```

--- Update ---

Please disregard the first sentence, thank you.
• February 23rd, 2014, 01:47 PM
GregBrannon
Re: Average Rainfall Main Class Using nested for loops,input validation - Average is off
Welcome to the forum! Thanks for taking the time to learn to post code correctly.

Study this and determine how it could contribute to your complaint:
Code java:

```while ( rainInches < 0 ) { System.out.println("Invalid. Enter 0 or greater:"); rainInches = keyboard.nextInt(); totalInches += rainInches; } totalInches += rainInches;```
• February 23rd, 2014, 01:58 PM
CyberOps
Re: Average Rainfall Main Class Using nested for loops,input validation - Average is off
All I had to do was remove totalInches += rainInches; from the last while statement. Wow thank you! I have spent hours learning to write this code.
• February 24th, 2014, 04:36 AM
GregBrannon
Re: Average Rainfall Main Class Using nested for loops,input validation - Average is off
You're welcome.