Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

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

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    Thank you for your help and advice.


    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.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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:
    while ( rainInches < 0 )
    {
        System.out.println("Invalid. Enter 0 or greater:");
        rainInches = keyboard.nextInt();
        totalInches += rainInches;
    }   
    totalInches += rainInches;

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Average Rainfall Main Class Using nested for loops,input validation - Average is off

    You're welcome.

Similar Threads

  1. creating a rainfall class
    By brayanG22 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 11th, 2013, 02:28 PM
  2. Average length of time for input/output
    By ManInTheMiddle in forum Threads
    Replies: 33
    Last Post: April 4th, 2013, 03:24 PM
  3. Using FOR and WHILE loops to find average (DUE TONIGHT)
    By cbh793 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 1st, 2013, 08:56 PM
  4. Loops: Taking the average of a list of numbers
    By janeeatsdonuts in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 24th, 2013, 09:38 AM
  5. HELPP!!!! - Java validation with nested loops
    By shinju in forum Loops & Control Statements
    Replies: 2
    Last Post: October 16th, 2012, 11:56 AM