0

I am not sure how i can pass the whole array through the methods calculateAverage, calculateHighScore and calculateLowScore. I tried doing a 2-d array but it was unsuccessful.

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
	Cannot make a static reference to the non-static field highestGrade1
	Cannot make a static reference to the non-static field highestGrade2
	Cannot make a static reference to the non-static field highestGrade3
	Cannot make a static reference to the non-static field highestGrade4
	Cannot make a static reference to the non-static field highestGrade5
	Syntax error on token ")", delete this token
	Cannot make a static reference to the non-static field lowestGrade1
	Cannot make a static reference to the non-static field lowestGrade2
	Cannot make a static reference to the non-static field lowestGrade3
	Cannot make a static reference to the non-static field lowestGrade4
	Cannot make a static reference to the non-static field lowestGrade5
	Cannot make a static reference to the non-static field a1
	Cannot make a static reference to the non-static field a2
	Cannot make a static reference to the non-static field a3
	Cannot make a static reference to the non-static field a4
	Cannot make a static reference to the non-static field a5

	at GradeBook.main(GradeBook.java:132)
import java.io.FileNotFoundException;
	import java.io.FileReader;
	import java.io.PrintWriter;
	import java.util.ArrayList;
	import java.util.List;
import java.util.Scanner;
	
	
	public class GradeBook {
		int lowestGrade1=0;
		int lowestGrade2=0;
		int lowestGrade3=0;
		int lowestGrade4=0;
		int lowestGrade5=0;
		int highestGrade1=0;
		int highestGrade2=0;
		int highestGrade3=0;
		int highestGrade4=0;
		int highestGrade5=0;
		int a1=0;
		int a2=0;
		int a3=0;
		int a4=0;
		int a5=0;
		public GradeBook(ArrayList<Integer> sid,ArrayList<Integer> q1,ArrayList<Integer> q2,ArrayList<Integer> q3,ArrayList<Integer> q4,ArrayList<Integer> q5){
		}
		public void calculateAverage(ArrayList<Integer> q1,ArrayList<Integer> q2,ArrayList<Integer> q3,ArrayList<Integer> q4,ArrayList<Integer> q5){
			int t1=0;
			int t2=0;
			int t3=0;
			int t4=0;
			int t5=0;
			for(int a=0; a<q1.size();a++){
					t1 += q1.get(a);
					a1=t1/q1.size();
					t2 += q2.get(a);
					a2=t2/q2.size();
					t3 += q3.get(a);
					a3=t3/q3.size();
					t4 += q4.get(a);
					a4=t4/q4.size();
					t5 += q5.get(a);
					a5=t5/q5.size();
					}		
			}
		public void calculatehighScore(ArrayList<Integer> q1,ArrayList<Integer> q2,ArrayList<Integer> q3,ArrayList<Integer> q4,ArrayList<Integer> q5){
			int h1= q1.get(0);
			int h2= q2.get(0);
			int h3= q3.get(0);
			int h4= q4.get(0);
			int h5= q5.get(0);
				for(int c=1;c<q1.size();c++){
					if (q1.get(c)>h1){
						h1= q1.get(c);
					}
					if (q2.get(c)>h2){
						h2= q2.get(c);
					}
					if (q3.get(c)>h3){
						h3= q3.get(c);
					}
					if (q4.get(c)>h4){
						h4= q4.get(c);
					}
					if (q5.get(c)>h5){
						h5= q5.get(c);
					}
					highestGrade1=h1;
					highestGrade2=h2;
					highestGrade3=h3;
					highestGrade4=h4;
					highestGrade5=h5;
				}
		}
			public void lowScore(ArrayList<Integer> q1,ArrayList<Integer> q2,ArrayList<Integer> q3,ArrayList<Integer> q4,ArrayList<Integer> q5){
				int l1= q1.get(0);
				int l2= q2.get(0);
				int l3= q3.get(0);
				int l4= q4.get(0);
				int l5= q5.get(0);
					for(int c=1;c<q1.size();c++){
						if (q1.get(c)<l1){
							l1= q1.get(c);
						}
						if (q2.get(c)<l2){
							l2= q2.get(c);
						}
						if (q3.get(c)<l3){
							l3= q3.get(c);
						}
						if (q4.get(c)<l4){
							l4= q4.get(c);
						}
						if (q5.get(c)<l5){
							l5= q5.get(c);
						}
						lowestGrade1=l1;
						lowestGrade2=l2;
						lowestGrade3=l3;
						lowestGrade4=l4;
						lowestGrade5=l5;
					}
			}
		public static void main(String[] args) throws FileNotFoundException
		{
			Scanner inFile= new Scanner(new FileReader("c:/GradeBook.txt"));
			inFile.findInLine("Stud Qu1 Qu2 Qu3 Qu4 Qu5 ");
			ArrayList<Integer> sid= new ArrayList<Integer>();
			ArrayList<Integer> q1= new ArrayList<Integer>();
			ArrayList<Integer> q2= new ArrayList<Integer>();
			ArrayList<Integer> q3= new ArrayList<Integer>();
			ArrayList<Integer> q4= new ArrayList<Integer>();
			ArrayList<Integer> q5= new ArrayList<Integer>();
			int i=0;
			while (inFile.hasNextLine() && i<15)
			{
				sid.add(i,inFile.nextInt());
				q1.add(i,inFile.nextInt());
				q2.add(i,inFile.nextInt());
				q3.add(i,inFile.nextInt());
				q4.add(i,inFile.nextInt());
				q5.add(i,inFile.nextInt());
				i++;
			}
			inFile.close();
			PrintWriter outFile= new PrintWriter("c:/GradeBook.out");
			outFile.println("Stud  Q1  Q2  Q3  Q4  Q5");
			System.out.println(sid.get(3));
			for(int b=0; b<sid.size();b++){
				outFile.printf("%2s %3s %3s %3s %3s %3s %n",sid.get(b),q1.get(b),q2.get(b),q3.get(b),q4.get(b),q5.get(b));
			}
			//	outFile.printf("High Score %s %s %s %s %s\n",highestGrade1,highestGrade2,highestGrade3,highestGrade4,highestGrade5));
			//		outFile.printf("Low Score: %s %s %s %s %s\n",lowestGrade1,lowestGrade2,lowestGrade3,lowestGrade4,lowestGrade5);
			//	outFile.printf("Average score:%s %s %s %s %s ",a1,a2,a3,a4,a5);
					outFile.close();
		}
		
	}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by NormR1
0

Cannot make a static reference to the non-static field

Looks like you are trying to reference variables from the static main() method that only exist when an object is created. The variables do NOT exist until you do a new GradeBook(). Once there is an object, you'll need to use the reference to that object to get at the variable in that object:
GradeBook gb = new GradeBook();
gb.highestGrade1 = ...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.