Skip to main content

Java Programming Homework Help

1 reply [Last post]
spidermonkey21
Offline
Joined: 2006-02-14
Points: 0

Hello,
I am a new user to this forum and I was wondering if anyone could help me with a compile problem I have been running in to.
The program is used for averaging grades. It allows the user to input as many as 1,000 grades, and then gets the average.

Whenever I compile the program, it comes up with two errors that I am unable to solve.

Here are the two errors it keeps telling me:

G:\AveragingGradesDialog.java:64: operator + cannot be applied to double,double[]
avgGrade += allGrades; //adds all grades together
^
(the arrow is below the + sign)

G:\AveragingGradesDialog.java:67: operator / cannot be applied to double,double[]
avg = avgGrade / allGrades; //averages grades
^
2 errors
(the arrow is below the / sign)

Tool completed with exit code 1

Here is the code

import javax.swing.JOptionPane;

public class AveragingGradesDialog
{
public static void main(String[] args)
{

double total, Grades, avgScore, prcent;

prcent = getGrade(); //percentage grade from getGrade method
avgScore = average(prcent); //average score from average method

}
public static double getGrade()
{
double prcent = 0, scoreTotal = 0;

boolean done = false;

while (!done) //starts loop
{
String score = JOptionPane.showInputDialog(null,
"Enter a grade (or -1 to quit): ");

if (score == null) finish(); //cancel button ends program
if (score == "-1") done = true; // -1 entered ends program

try
{
prcent = Double.parseDouble(score); //changes string score to double
if ((prcent <= -2)||(prcent == 0)) throw new NumberFormatException(); //bad numbers
else done = true; //ends loop
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null,
"Entry must be numberic and between 1 and 1000","Error",
JOptionPane.INFORMATION_MESSAGE);
}
}

return prcent; //returns percent
}
public static double average (double avgGrade)
{

double avg = 0, sumGrades;
double [] allGrades = new double[1000]; //array with 1000 spaces

for(int i=0; i<=allGrades.length; i++)
{
sumGrades += avgGrade;
avgGrade += allGrades; //adds all grades together

}
avg = avgGrade / allGrades; //averages grades
JOptionPane.showMessageDialog(null,
"Your average score is "+ avg+"."); //shows user average grade

return avg; //returns average
}

public static void finish()
{
System.exit(0);
}
}

If someone could help me, i would greatly appreciate it, Thank You

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
destin
Offline
Joined: 2006-05-05
Points: 0

This is because allGrades is an array. Change your loop to this:
[code]for (int i = 0; i < allGrades.length; i++) {
sumGrades += avgGrade;
avgGrade += allGrades[i]; // add allGrades at index i to avgGrade
}[/code]