I can't get this to compile. It is for a homework assignment. The program asks user for 5 test scores and is to display letter grade and average test score. I was able to get the calcAverage method to work, but after adding the determineGrade method, I end up with errors.

Could you please lend some assistance to help me solve this? THX in advance!

import javax.swing.JOptionPane;		//Needed for dialog boxes
import java.io.*;

public class TestGradeAvg
{
		public static void main(String [] args) throws IOException
		{
		String input;
		double total;
		int gradeCounter;
		double score;
		double avgScore;
		
		
		//initialize
		total = 0;
		gradeCounter = 1;
		
		//process / get user score
		while (gradeCounter <= 5)
			{
			input = JOptionPane.showInputDialog("Enter score.");
			score = Double.parseDouble(input);
			total = total + score;
			gradeCounter = gradeCounter + 1;
			}
			//Main method calling calcAverage(avgScore);	
			calcAverage(avgScore);
			avgScore = total / 5.0;
		
						
			//Main method calling determineGrade method

			char grade = determineGrade(score);
			
						
			//determineGrade method (based on input)
			}
			public static char determineGrade(double score)
			{
			if (score >= 90.0) { return 'A'; } 
			else if (score >= 80.0) { return 'B'; } 
			else if (score >= 70.0) { return 'C'; } 
			else if (score >= 60.0) { return 'D'; } 
			else { 
			return 'F'; 
			JOptionPane.showMessageDialog(null, "Your grade is " + grade);
			}
						
			//calcAverage method						
			}
			public static void calcAverage(double avgScore)
			{
			JOptionPane.showMessageDialog(null, "Your average score is " + avgScore);
			}
		}

Recommended Answers

All 8 Replies

Why are you defining TestGradeAvg
twice ?

First of all what kind of errors do you get and where?
And from what I see:
In calcAverage() you simply display the average, so in the main I think that you should first call the avgScore = total / 5.0; and then the calcAverage();
And second, the determineGrade() returns a char. If you see the code, the JOptionPane.showMessageDialog(null, "Your grade is " + grade); is inside the last else {},so when the grade is for example 90 it will return 'A', but if it is 50 then it will return 'F' and the JOptionPane.showMessageDialog(null, "Your grade is " + grade); will never be executed:

public static char determineGrade(double score)
			{
			if (score >= 90.0) { return 'A'; } 
			else if (score >= 80.0) { return 'B'; } 
			else if (score >= 70.0) { return 'C'; } 
			else if (score >= 60.0) { return 'D'; } 
			else { 
			return 'F'; 
			JOptionPane.showMessageDialog(null, "Your grade is " + grade);
			}

As you can see when the red return 'F' will executed the method will end. And by placing JOptionPane.showMessageDialog(null, "Your grade is " + grade); inside the last else{} is like saying that you want to display the grade only if the student has failed.

You have the following options:
Put the JOptionPane.showMessageDialog(null, "Your grade is " + grade); before the return statement inside all the ifs (not so good repeating code) or remove it from the method and do this:

public static char determineGrade(double score)
			{
			if (score >= 90.0) { return 'A'; } 
			else if (score >= 80.0) { return 'B'; } 
			else if (score >= 70.0) { return 'C'; } 
			else if (score >= 60.0) { return 'D'; } 
			else { return 'F'; }
}

and call it in the main like this:

char grade=determineGrade(score);
  JOptionPane.showMessageDialog(null, "Your grade is " + grade);

Thank you. A lot of times, I have the right code, but out of place.

I've moved the JOptionPane to the main and then had to initialize score variable to 0. However, I'm only getting one grade overall. I'm trying to get the determineGrade method to accept a test score as an argument and then return a letter grade for the score.

Any suggestions.

Your determineGrade already does that:

public static char determineGrade(double score)
      {
      if (score >= 90.0) { return 'A'; }
      else if (score >= 80.0) { return 'B'; }
      else if (score >= 70.0) { return 'C'; }
      else if (score >= 60.0) { return 'D'; }
      else {
         return 'F';
      //JOptionPane.showMessageDialog(null, "Your grade is " + grade);
      }
}

Takes the score as argument and returns the grade. Just remove the line I have commented out: //JOptionPane.showMessageDialog(null, "Your grade is " + grade);

Yes - I removed the specified line, but I only get the grade for the final test score I enter. I can input 4 scores @ 100 and 1 score @ 50 and I'm getting "Your grade is F". It should display each grade for each score.
What am I doing wrong? THX!

Call determineGrade followed by the JOptionPane.showmessage() more times (after every time you input the grades). If this doesn't satisfy then you have the methods that work, so use them in order to do whatever you like

Thank you so very, very much. I appreciate your guidance and patience as I've only been at this Java coding a very short while...

Have a great day!

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

}

also, don't do that. there is nothing above the main method in the any application, so where would you throw it to?

you'd better replace that by

try{
// what you want to happen
}
catch(IOException ex){
// generate error msg
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.