Problem on my code


Hello Everyone. I'm sharing my code below. The problem it says " error: missing return statement
Note: uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error"
Is there anybody to help me ?

import java.util.ArrayList;
//Next, create a class called GradeAnalyzer.
class GradeAnalyzer{
//Now create a GradeAnalyzer constructor. You can leave the contents of the constructor empty.
public GradeAnalyzer(){}
//Let's create a method that will return the average of all grades. Create a method called getAverage. It should return an int.
public int getAverage(ArrayListgrades){
//The first thing the method should do is check to see that the ArrayList it's analyzing is not empty. Create an if statement that checks if the size of grades is less than 1.
if(grades.size() < 1){
//Inside of the if block, print out a friendly error message to the user indicating that the ArrayList is empty. On the next line, return 0.
System.out.println("The ArrayList cannot be empty.");
// Otherwise, in an else block, find the average of all grades. To find the average, first we will need the sum of all grades. Create an int called sum and set it equal to 0. We will update this variable as we sum the grades.
int sum = 0;
//Create a for each block that iterates through each grade in the grades ArrayList.
for (Integer grade : grades){
//Inside of the for each block, update sum. Set it equal to sum plus grade. This for each loop will add up all the grades.
//Outside of the for each loop, calculate the average of the grades. Create an int called average and set it equal to the sum divided by the size of grades.
int average = sum/grades.size();
//On the next line, print out the average.
System.out.println("The average is "+ average+".");
//Finally, on the next line, return the average.
return average;
//Next, create the main method for this program.
public static void main(String[] args){
//Inside of main create an ArrayList object that stores integers and call it myClassroom.
ArrayList myClassroom = new ArrayList();
//Next, add the grades 98, 92, 88, 75, 61, 89 and 95 to myClassroom.
//On the next line, create a GradeAnalyzer object called myAnalyzer.
GradeAnalyzer myAnalyzer = new GradeAnalyzer();
//Now call the getAverage method on myAnalyzer and specify myClassroom as the argument (parameter).


If your function signature says that your function returns a non-void value, then your function has to return something.

Make sure that the code displays correctly in your post or it gets really difficult to read.

The error that you describe is not what I get when I compile the code you posted, did you post the right code?

This is what I get:

[nate@localhost sf_Code]$ javac error: <identifier> expected
    public int getAverage(ArrayListgrades){
1 error

That has probably got to do with the formatting of your post, so not only does formatting your code correctly in your post make it readable, it makes sure that the code is intact!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.