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

Thread: Average of an array! help and explanation much appreciated! :)

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Average of an array! help and explanation much appreciated! :)

    public class Apples{
        public static void main (String args[]){
        	int array[]={21,16,86,21,3};
        	int sum=0;
        	int average;
     
        	for(int counter=0;counter<array.length;counter++){
        		sum*=array[counter];
        		average = sum/array.length;
        	}
        	System.out.println("The average of these numbers =" + average);
        	}
       }

    Eclipse: Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The local variable average may not have been initialized

    at Apples.main(Apples.java:11)


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,332
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default Re: Average of an array! help and explanation much appreciated! :)

    This is an example of Definite assignment analysis - Wikipedia, the free encyclopedia

    As a general rule, when a compiler error tells you something has not been done yet (in this case 'average' has not been initialized), then its probably best to do what it suggests.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Average of an array! help and explanation much appreciated! :)

    im a beginner and i didnt understand most of the link you embedded, would you mind explaining in a bit more detail please?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,985
    Thanks
    52
    Thanked 2,271 Times in 2,243 Posts

    Default Re: Average of an array! help and explanation much appreciated! :)

    variable average may not have been initialized
    The compiler is asking you to give the variable an initial value.
    If you don't understand my answer, don't ignore it, ask a question.

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

    copeg (May 14th, 2014)

  6. #5
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Average of an array! help and explanation much appreciated! :)

    public class Apples{
        public static void main (String args[]){
        	int bucky[]={21,16,86,21,3};
        	int sum=0;
        	int average=sum;
     
        	for(int counter=0;counter<bucky.length;counter++){
        		sum+=bucky[counter];
        		average=sum/bucky.length;
     
       }
    	System.out.println("The sum of the numbers = " + sum);
    	System.out.print(average);
    	}
    }
    i think this is better, correct?

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,985
    Thanks
    52
    Thanked 2,271 Times in 2,243 Posts

    Default Re: Average of an array! help and explanation much appreciated! :)

    Why assign sum to average, why not 0?

    i think this is better
    What does the compiler think?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Average of an array! help and explanation much appreciated! :)

    all class variables are initialized in class instatiation phase that mean when object is created class level variables are initialized by their default values but in case of local variable "When you declare any local/block variable, they didn’t get the default values. They must assigned some value before accessing it other wise compiler will throw an error."
    and your average is local variable in ur program so you have to initialized it

Similar Threads

  1. How do I get an average using an Array?
    By cheshire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 22nd, 2014, 05:46 AM
  2. use array of scores to calculate an average
    By littlebit45 in forum Other Programming Languages
    Replies: 1
    Last Post: November 30th, 2012, 02:25 PM
  3. Replies: 2
    Last Post: October 23rd, 2012, 09:37 AM
  4. Replies: 1
    Last Post: October 22nd, 2012, 07:19 PM
  5. Calculating average of an array
    By Vika in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 29th, 2011, 08:06 AM