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 2 of 2

Thread: Finding the Average

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Finding the Average

    I'm trying to find the average of "pressure" in the code. However, "pressure" is an array. My main question is: how would I find the average of the numbers in an array?
    In the code, there area that contains the array "pressure" is all the way towards the bottom.

    import java.util.Scanner;
    import java.io.File;
    import java.io.IOException;
    public class Hurricanes2
    {
        public static void main(String[] args) throws IOException
        {
            Scanner inFile = new Scanner(new File("hurcdata2.txt"));
            int numLines = 0;
     
     
            while(inFile.hasNextLine()) 
            { 
                inFile.nextLine(); 
                numLines++; 
            } 
            inFile.close(); 
            inFile = new Scanner(new File("hurcdata2.txt")); 
     
            int [] years = new int[numLines]; 
            String [] name = new String [numLines];
            int [] category = new int [numLines];
            int [] pressure = new int [numLines];
            double [] wind = new double [numLines];
            String [] month = new String [numLines];
     
            System.out.printf("%45s\n", "Hurricanes 1980 - 2006");
            System.out.println();
            System.out.println("Year     Hurricane     Category     Pressure (mb)     Wind Speed (mph)");
            System.out.println("=======================================================================");
     
            int i=0; 
            while(inFile.hasNext()) 
            { 
                years[i] = inFile.nextInt(); 
                month[i] = inFile.next(); 
                pressure[i] = inFile.nextInt(); 
                wind[i] = inFile.nextDouble(); 
                name[i] = inFile.next(); 
                i++; 
            } 
            inFile.close(); 
     
            int cat1 = 0;
            int totalCat = 0;
     
            for(int m = 0; m < wind.length; m++)
            {
                wind[m] = wind[m] * 1.15;
     
     
                if(wind[m]>=74 && wind[m]<=95)
                {
                   category[m] = 1;
                   totalCat++;
                }
                else if(wind[m]>=96 && wind[m]<=110)
                {
                   category[m] = 2;
                   totalCat+=2;
                }
                else if(wind[m]>=111 && wind[m]<=130)
                { 
                   category[m] = 3;
                   totalCat+=3;
                }  
                else if(wind[m]>=131 && wind[m]<=155)
                {  
                   category[m] = 4;
                   totalCat+=4;
                }
                else
                {
                   category[m] = 5;
                   totalCat+=5;
                } 
     
     
            }
     
            double avgCategory = (double)totalCat/category.length;
            int totalPres = 0;
     
            for(int p = 0; p < pressure.length; p++)
            {
                totalPres++;
            }
     
            double avgPressure = (double)totalPres/pressure.length;
     
     
            for(int n = 0; n < years.length; n++)
            {
                System.out.printf("%1d %12s %10d %15d %20.2f%n", years[n], name[n], category[n], pressure[n], wind[n]);
            }
     
            System.out.println("========================================================================");
            System.out.print("Average");
            System.out.printf("%21.0f %10.0f", avgCategory, avgPressure);
        }
    }


  2. #2
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Finding the Average

    What you would want to do is loop through all of the array's elements and adding them together, then divide by the amount of elements.

    int average = 0;
    for (int i = 0; i < pressure.length; i++) {
       average += pressure[i];
    }
    average /= pressure.length;
    Last edited by Brt93yoda; September 16th, 2010 at 09:00 PM.

  3. The Following User Says Thank You to Brt93yoda For This Useful Post:

    KiwiFlan (September 16th, 2010)

Similar Threads

  1. How to find location of ipaddress using Java
    By tej in forum Java Networking
    Replies: 8
    Last Post: September 8th, 2012, 07:05 AM
  2. [SOLVED] Finding acm package
    By javapenguin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 12th, 2010, 12:50 PM
  3. Moving average
    By bondage in forum Loops & Control Statements
    Replies: 0
    Last Post: May 7th, 2010, 05:20 AM
  4. Average program with array and loop and JOptionPane.
    By jeremykatz in forum AWT / Java Swing
    Replies: 6
    Last Post: October 25th, 2009, 03:33 PM
  5. Replies: 13
    Last Post: May 6th, 2009, 10:27 AM