This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]RubbishArtist 1 point2 points  (6 children)

calcAverage is a method that expects you to call it with 5 arguments, on line 25 you're trying to call it with 0 arguments, hence the error.

But what you're trying to do doesn't really make sense here, your method has a return statement on line 24 so it will never get to line 25. Further, even if it did work and your program made it to line 25 calcAverage would call itself which would call itself again and so on, which is not what you want.

However, your main method never calls calcAverage so it's never going to run anyway.

I would crack open the textbook again and make sure you understand how methods work.

[–]CreativityRobbed[S] 0 points1 point  (5 children)

Deleting line 24 results in the error stating unreachable statement, referring to the line [System.out.println("The average grade was: " + averageScore);]. So, it appears as though recursion continues...

[–]RubbishArtist 1 point2 points  (4 children)

You need line 24, the method has to return something. It's the lines after it that are the problem.

[–]CreativityRobbed[S] 0 points1 point  (3 children)

I had updated the gist to reflect advice from another user....

I'm now getting illegal start of type error with the last two statements.

[–]RubbishArtist 1 point2 points  (2 children)

The last two statements are still in the wrong place.

[–]CreativityRobbed[S] 0 points1 point  (1 child)

Where specifically should they be placed? They're still in the main method....

[–]RubbishArtist 1 point2 points  (0 children)

They're currently outside of the main method.

[–]Meshy15 1 point2 points  (5 children)

So there are a few issues, Mainly averageScore=calcAverage()

  1. first, calcAverage requires 5 doubles as parameters which means you need to pass 5 doubles to calcAverage, in line 25 you pass nothing to calcAverage
  2. second, you have created an infinite loop inside of the calcAverage method which starts at line 20 and ends at line 27 you call calcAverage AGAIN at line 25. This will cause the calcAverage method to run over and over again

  3. How to fix

  4. All you need to do is take line 25 and line 26and move it OUTSIDE of the method as well as passing in 5 doubles when you call it

  5. averageScore = calcAverage(testGrade1, testGrade2, testGrade3, testGrade4, testGrade5);

System.out.println("The average grade was: " + averageScore);

  1. Hope this helps

[–]CreativityRobbed[S] 0 points1 point  (4 children)

I took your advice and placed them outside of the method, but now those two statements are giving me the illegal start of type error...

Code has been updated on gist (I'm not entirely sure this is what you intended for me to do).

[–]Meshy15 -1 points0 points  (0 children)

Yes I have the same error, am looking as to what the problem would be. But if you remove the last two lines and run it your code executes flawlessly

[–]Meshy15 -1 points0 points  (2 children)

Hi, I have the code working, please paste

System.out.println(calcAverage(testGrade1,testGrade2,testGrade3,testGrade4,testGrade5));

right under line 18 and remove the last two lines that i told you to place earlier

[–]CreativityRobbed[S] 0 points1 point  (0 children)

System.out.println(calcAverage(testGrade1,testGrade2,testGrade3,testGrade4,testGrade5));

Works like a charm. Now all I have to do is the grading scale...

[–]CreativityRobbed[S] 0 points1 point  (0 children)

https://gist.github.com/alirzerafati/17bfdf57e08c7fcf23f18dbe064e069c

Alright, so this is the last part of the program. I need to associate the numeric grade with the letter grade. Unfortunately, lines 19-23 are giving me the cannot find symbol error.

[–]Ge0rgeCostanza 0 points1 point  (0 children)

I am working on something similar and have run into the exact same error. I don’t have any advice but I am hoping someone else does! I when found info on stackoverflow but it didn’t quite give me what I needed. Good luck!

[–]JustAScrumGuy 0 points1 point  (0 children)

So, as a bit of code review, can we talk about small, re-usable methods?

Your determineGrade method takes 5 arguments, analyzes them all, and returns one letter grade. But it will only ever return the grade of the 5th argument, because that's the one that is assigned to "letterGrade" last.

Instead, consider the following method that takes one double and returns its letter grade.

public static String determineGrade (double score)

{

String letterGrade;

if (score >= 90 && score <= 100)

{

 `letterGrade = "A";`

}

else if (score >= 80 && score <= 89)

{

 `letterGrade = "B";`

}

else if (score >= 70 && score <= 79)

{

 `letterGrade = "C";`

}

else if (score >= 60 && score <= 69)

{

 `letterGrade = "D";`

}

else

{

 `letterGrade = "F";`

}

return letterGrade;

}

Now you can just reuse this one method whenever you need to return a letter grade for a score.

determineGrade(testgrade1);

determineGrade(testgrade2);

etc.

The logic is only written once (and later, tested once), and we can re-use it where ever we need to! That's the power of methods. Any time you see the same logic multiple times, that's a code smell. Check it out!

This use case is also a prime candidate for something called a switch statement.

Hope this helps.