Seem to be having trouble. The trouble i'm having is the calculateAverage, and a bit of the public static main void area. I'll explain:

calculateAverage: What i'm suppose to do is read from the txt file. I tried doing the inFile.nextDouble part, but seem to be getting errors. i'm not sure if I have to input parameters like calculateGrade. Or somehow involve the scanner code. Or try using a loop method.

Therefore my objective on this part: read the five scores from input file.

calculateGrade: 100% fine with this, so skip.

main method: Line51: i'm suppose to set average to call of calculateAverage, as well as calculateGrade. would I have to do something like...
sAverage = calculateAverage();?
sGrade = calculateGrade(average);.


That's pretty much it. It's the average variable that seems to be my issue. It just needs a few tweaks on that part as well as the main method area, and then I should be set. Any help is appreciated. Just need a point to the right direction Here is my code:

import java.util.*;
import java.io.*;

public class Grades {

		public static double calculateAverage()
{
		average = ((Test1 + Test2 + Test3 + Test4 + Test5) / 5.0);
		return average;
				}

		public static String calculateGrade(double average)
		{

		  if (average >= 90)
			   return "A";
		  else if (average >= 80)
			   return "B";
		  else if (average >= 70)
			   return "C";
		  else if (average >= 60)
			   return "D";
		  else
			   return "F";
				          }

	public static void main (String [] args) throws FileNotFoundException {
		String grade = "";
		int count = 0;
		double sum = 0.0;
		String Name;
		String sAvgGrade = "";
		double Test1, Test2, Test3, Test4, Test5, sAverage, classAverage;


	System.out.println("Student	Test1 Test2 Test3 Test4 Test5 Average Grade");

	Scanner inFile = new Scanner(new FileReader ("Students.txt"));

		while(inFile.hasNext())
		{

			Name = inFile.nextLine();
			System.out.println(Name);
			Test1 = inFile.nextDouble();
			Test2 = inFile.nextDouble();
			Test3 = inFile.nextDouble();
			Test4 = inFile.nextDouble();
			Test5 = inFile.nextDouble();

		//Confused here;

		System.out.printf("%2d %s\n", average, grade);
			sum = sum + sAverage; //***Need to set sAverage***
			count++;
		System.out.println("");
		System.out.println("Class Average = "  + sum/count);
		inFile.close();
	}
}
}

This is pretty simple code but I see some errors here and there. Are you trying to calculate the averages and grade in a seperate method or all in the main method?

For the average calc method it should look like this:
public static double calculateAverage(double Test1, double Test2, etc.)

And when you call that method you use:
Double Average = calculateAverage(Test1, Test2, etc.);

I'll give you a quick run down of what does what in the method title:
public -> accessible anywhere
static -> use no instance variables
double -> data type method will return
title
Parentheses -> data the method receives to be manipulated

Edited 5 Years Ago by Slyvr: n/a

This is pretty simple code but I see some errors here and there. Are you trying to calculate the averages and grade in a seperate method or all in the main method?

For the average calc method it should look like this:
public static double calculateAverage(double Test1, double Test2, etc.)

And when you call that method you use:
Double Average = calculateAverage(Test1, Test2, etc.);

I'll give you a quick run down of what does what in the method title:
public -> accessible anywhere
static -> use no instance variables
double -> data type method will return
title
Parentheses -> data the method receives to be manipulated

Alright thanks for the reply. I was trying to calculate grade and average in separate methods then return them into the main method I believe :p Anywho, thanks for the info on public/static/double information.

Well ok, I worked it, in which I added an output file, and since then I have done great, with retweaking it with help from a student. Just one minor problem. On line 25, calculating classAverage. Basically getting all the averages from the student's score after the 5 tests, and inputting the total.

It's on line 25. Sum+= is what i'm stuck on. I know afterwards, I use sum/count but first I need to get the sum results. Little confused on that part. Once I figure this slight minor problem out, this java program is 100% finish, as everything works, the student's averages and the letter score. Therefore, any help appreciated. Thanks

import java.util.*;
import java.io.*;



public class Grades {

public static void main(String[] args) throws FileNotFoundException
	{

				Scanner inFile = new Scanner(new FileReader ("students.txt"));
				PrintWriter outFile = new PrintWriter("overallscore.txt");

				double Sum = 0.0;
				int count = 0;
				double classAverage = 0.0;

outFile.println("Student    Test1 Test2 Test3 Test4 Test5 Average Grade");


		while(inFile.hasNext())
			{
				count++;
				calculateAverage(inFile, outFile);
				Sum+=;//need help

			}

			 if (count > 0)
			 classAverage = Sum / count;

		outFile.println("\n\nClass Average = " + classAverage);


		inFile.close();
		outFile.close();


			}

			public static void calculateAverage(Scanner iFile, PrintWriter oFile)
			{
				String name;
				int test;
				double average;
				String grade;
				int sum;
				sum = 0;
				name = iFile.next();
				oFile.printf("%-9s  ", name);

				for (int count = 0; count < 5; count++)
				{
					test = iFile.nextInt();
					oFile.printf("%4d  ", test);
					sum = sum + test;

				}

				average = sum/5.0;
				grade = calculateGrade(average);
				oFile.printf("%5.1f  %3s", average, grade);
				oFile.printf("%n");


			}


			private static String calculateGrade(double average)
			       {
			       	if (average >= 90)
			       		return "A";
			       	else if (average >= 80)
			       		return "B";
			       	else if (average >= 70)
			       		return "C";
			       	else if (average >= 60)
			       		return "D";
			       	else
			       		return "F";
		          }

		}

It shouldn't be in the while loop.
What does the file look like? A list of numbers seperated by spaces? newlines? commas?

this is what the file looks like after compiling, the output.

Student    Test1 Test2 Test3 Test4 Test5 Average Grade
Johnson      85    83    77    91    76   82.4    B
Aniston      80    90    95    93    48   81.2    B
Cooper       78    81    11    90    73   66.6    D
Gupta        92    83    30    69    87   72.2    C
Blair        23    45    96    38    59   52.2    F
Clark        60    85    45    39    67   59.2    F
Kennedy      77    31    52    74    83   63.4    D
Bronson      93    94    89    77    97   90.0    A
Sunny        79    85    28    93    82   73.4    C
Smith        85    72    49    75    63   68.8    D


Class Average = 0.0

This is my input file

Johnson	85	83	77	91	76
Aniston	80	90	95	93	48
Cooper	78	81	11	90	73
Gupta	92	83	30	69	87
Blair	23	45	96	38	59
Clark	60	85	45	39	67
Kennedy	77	31	52	74	83
Bronson	93	94	89	77	97
Sunny	79	85	28	93	82
Smith	85	72	49	75	63

I think the easiest way to do it:

#
public class Grades {
    int totalSum=0;
public static void main(String[] args) throws FileNotFoundException
#
for (int count = 0; count < 5; count++){
    test = iFile.nextInt();
    oFile.printf("%4d ", test);
    sum = sum + test;
    totalSum+=sum;
}
classAverage = totalSum / count;

Heya, thanks for the reply for helping me out. I tried but it's giving me an error of "non-static variable totalSum cannot be referenced from a static context". Well anywho, i'll bound to get this soon, i'll just mess around a bit with the program. Will be going to comp lab to seek help if i'm still not getting it right.

Much appreciated.

Ok, finally got it working again. Much appreciated! :P Solved!

This question has already been answered. Start a new discussion instead.