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

Thread: Calculating average of an array

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Calculating average of an array

    Have written a program for my uni assignment that stores information about fitness club members(such as name, age weight, BMI index etc.) One of the operations that program must perform is to calculate an average BMI index. The method I have written returns value of 0 and can't figure out where the problem lays.

    public double averageBmi()//to calculate average BMI index
    	{
    		double numbers[]=new double[this.list.length];
    		double result=0;
    		for(int i=0;i<numbers.length;i++){
    			double b=this.list[i].Bmiindex;
    			result=result+numbers[i];
    		}
     
    		System.out.println("Average BMI is "+result/numbers.length);
    		return result;
    	}
    Any ideas why?


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Calculating average of an array

    numbers is empty !!! wath is double b ?

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calculating average of an array

    double b is accessing list of all Bmiindexes stored in another class

  4. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Calculating average of an array

    but number[] is still empty 0+0=0
    meybe rezult=rezult+b

  5. The Following User Says Thank You to bojanm For This Useful Post:

    Vika (March 27th, 2011)

  6. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calculating average of an array

    It works now! Thanks a lot!

  7. #6
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calculating average of an array

    Facing the same issue with the method calculating average age for each group
    public double averageAge() // 
    	{
    		double numbers[]=new double[this.list.length];
    		double result=0;
    		char[] cat={'O','U','N'};
    		for (int i=0;i<this.list.length;i++){
    			int b=this.list[i].age;
    			for(int j=0;j<3;j++){//loop runs three times, as there are three categories
    				if(b == cat[j])
    			result=result+b;
    		}
    }       for (int j=0;j<3;j++) 
    		System.out.println("Average age in group "+cat[j]+ " is "+result/numbers.length);
    		return result;
    }

  8. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Calculating average of an array

    I have removed your other thread as the content is duplicated.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  9. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Calculating average of an array

    1. Most probably, your list is throwing garbage instead of any value.
    2. You should use explicit type casting in calculation.

Similar Threads

  1. Calculating Price with Command Line - Please Help!
    By rjdelight in forum Object Oriented Programming
    Replies: 3
    Last Post: February 6th, 2011, 04:49 PM
  2. Calculating Business days in java
    By narranil2 in forum Algorithms & Recursion
    Replies: 2
    Last Post: August 17th, 2010, 12:08 PM
  3. question on calculating
    By meowCat in forum Java Theory & Questions
    Replies: 5
    Last Post: August 9th, 2010, 05:06 PM
  4. KB/s download speed calculating
    By Koâk in forum Java Theory & Questions
    Replies: 2
    Last Post: December 16th, 2009, 03:05 PM
  5. Average program with array and loop and JOptionPane.
    By jeremykatz in forum AWT / Java Swing
    Replies: 6
    Last Post: October 25th, 2009, 02:33 PM