I'm having some trouble trying to figure out how to assign a value to a variable being passed through a method parameter. First of all here is my relevant code:
public static void main(String[] args) throws FileNotFoundException { Scanner inFile = new Scanner(new FileReader("grades.txt")); PrintWriter outFile = new PrintWriter("output.txt"); String name; int gr1, gr2, gr3, gr4, gr5; double avg = 0; char letterGr; while (inFile.hasNext()) { name = inFile.next(); gr1 = inFile.nextInt(); gr2 = inFile.nextInt(); gr3 = inFile.nextInt(); gr4 = inFile.nextInt(); gr5 = inFile.nextInt(); getAverage(gr1, gr2, gr3, gr4, gr5, avg); letterGr = getGrade(avg); System.out.printf("%7s%7d%7d%7d%7d%7d%7.2f%7s%n", name, gr1, gr2, gr3, gr4, gr5, avg, letterGr); } } public static void getAverage(int g1, int g2, int g3, int g4, int g5, double average) { average = (g1 + g2 + g3 + g4 + g5) / 5; }
Now as it is, when I print it out, avg is always 0. For the method, getAverage, it has to be a void method, yet still set avg equal to the sum of the grades divided by 5 when the method is called. When my teacher explained this, it didn't seem too difficult but I just can't seem to figure out what I need to do. Normally I would just make this a method returning a double and assigning the value of the method to avg in main, but for this program assignment we have to do it this way.
So basically, I need help to pass the variable, avg, through the method as the average parameter, and then set avg equal to the calculation in the method if that makes any sense. Any help would be greatly appreciated and if you need any more info just ask.