I am working on my home work assignment and I am getting compile errors that I don not understand. Can you take a look and expalin what I am doing wrong please?
Create a program to calculate the average of any number of grades input into the array by the user, up to 10 grades. The user gets to decide how many, and may have 2 grades or 6, or even 10, for example. Include at least 3 methods:
• main, which declares the grades array, calls the other methods, and writes ouput of the grades and their average.
• get_grades, which gets user input of integer grades (You may assume they are 0 through 100 and that at least one grade is entered).
• calculate_average, which uses the grades array and passes the average back to main.
Outside of the compile errors I do not understand how to add up all the grades (the data in the array) together to make a sumOfAllGrades? Can somebody explain that please?
/*Create a program to calculate the average of any number of grades input into the array by the user, up to 10 grades. The user gets to decide how many, and may have 2 grades or 6, or even 10, for example. Include at least 3 methods: • main, which declares the grades array, calls the other methods, and writes ouput of the grades and their average. • get_grades, which gets user input of integer grades (You may assume they are 0 through 100 and that at least one grade is entered). • calculate_average, which uses the grades array and passes the average back to main. */ import java.io.*; import java.text.*; import java.util.*; public class secondHW4attempt { /*...This will define the size of the grades array, the size of the array is user defined.This is also a method....*/ public double howManyGrades () { int howManyGrades; String inputStr; inputStr = JOptionPane.showInputDialog(null, "How many grades do you wish to average? "); int howManyGrades = Integer.parseInt(inputStr); return howManyGrades; } /*...End method howManyGrades method.............................................................*/ /*...With this method we get get passed variable howManyGrades so that it can define the size of our array. We howManyGrades as a counter for our for_loop. Then each grade entered into the array is added for the sumOfGrades...*/ public double gradesArray(){ double grades, averageOfGrades, sumOfGrades; int howManyGrades; String inputStr; Scanner input = new Scanner(System.ini); double average = (howManyGrades + grade)/ 3.0; double[] grades = new double [howManyGrades]; for(int i = 0; i < grades.length; i++){ inputStr = JOptionPane.showInputDialog(null, "Enter a grade."); int grades[i] = Integer.parseInt(inputStr); /*??????????????????????????????????????????????????????????????????????????????????????????????? //sumOfGrades = grades //How do I add each grade in the array togeather so I can get a sumOfGrades ????????????????????????????????????????????????????????????????????????????????????????????*/ return sumOfGrades; } /*...End method gradesArray .............................................................................*/ /*...This is the calculateAverage method were we divide howManyGrades by sumOfGrades for the average and we output the average for the user...*/ public double calculateAverage(){ DecimalFormat df = new DecimalFormat("0.00"); double average; average = sumOfGrades / howManyGrades; if (howManyGrades != 0) { average = (double) total/howManyGrades; JOptionPane.showMessageDialog(null, "The average is " + df.format(average) + "\n"); } // end if else JOptionPane.showMessageDialog(null, "No grades were entered. "); //return average; } /*...End method calculateAverage method.............................................................*/ public static void main(String[] args) { workingHomeWork4 clerk = new workingHomeWork4(); clerk.howManyGrades( ); clerk.gradesArray( ); clerk.calculateAverage( ); } // end main } //end class
Compile Errors
----jGRASP exec: javac -g C:\Documents and Settings\jeremy\Desktop\SCHOOL\CMIS 141\week 9\secondHW4atempt.java
secondHW4atempt.java:39: ']' expected
int grades[i] = Integer.parseInt(inputStr);
^
secondHW4atempt.java:39: illegal start of expression
int grades[i] = Integer.parseInt(inputStr);
^
secondHW4atempt.java:53: illegal start of expression
public double calculateAverage(){
^
secondHW4atempt.java:53: ';' expected
public double calculateAverage(){
^
secondHW4atempt.java:73: illegal start of expression
public static void main(String[] args) {
^
secondHW4atempt.java:73: illegal start of expression
public static void main(String[] args) {
^
secondHW4atempt.java:73: ';' expected
public static void main(String[] args) {
^
secondHW4atempt.java:73: '.class' expected
public static void main(String[] args) {
^
secondHW4atempt.java:73: ';' expected
public static void main(String[] args) {
^
secondHW4atempt.java:85: reached end of file while parsing
}
^
10 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.