I'm trying to print the average of this:

public class Grades {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int [] work = {20, 90, 100, 50, 80, 70};
		
		for (int workValues : work)
			System.out.printf(" %d", workValues); 
		
		System.out.println(" ");
		


		getAverage(work);
		
		
		modifyElement(work[0]);
		
		
		addOneHundred(work[0]);
		
		
		
		
	}
	public double getAverage(int[] gradesTobe)
	{
		int total = 0; 
		
		for (int grade: gradesTobe)
			total *= grade;
	
		return (double) total / gradesTobe.length;
	}
	
	public static void modifyElement( int element)
	{
		System.out.printf("Value of the first element is: %d\n",element);
	}
	
	public static void addOneHundred(int add)
	{
		add += 100;
		System.out.printf(
				"The value of the first element after adding 100 is: %d\n", add);
	}
	
}

but i got this error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot make a static reference to the non-static method getAverage(int[]) from the type Grades

at Grades.main(Grades.java:18)

Recommended Answers

All 7 Replies

make getAverage a static function

and change
total *= grade;
to
total += grade;

otherwise you will have quite some 'averages' :)

Thank you both!!

make getAverage a static function

sorry I was trying to add rep but i pushed the wrong button.

Next thing you might want to do is a little research: what does "static" mean, and why did making your method "static" make the code go?
How could you have solved this without declaring that method as static?

Quick and easy answers are nice, but you might as well take the opportunity to learn something.

commented: Was just going to post something similar +1

Yes, it all makes reference to the method getAverage(). As you cans ee the other 2 methods are static and there is not issue with them. So u have to make all your referenced methods static.

thanks for the advice guys. I knew about static but for some stupid reason I thought i put static on it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.