| Author |
Creating a static method for calculating averages
|
Tommy Cregan
Greenhorn
Joined: Nov 11, 2012
Posts: 1
|
|
Hi all,
Ive been asked to create a class that has a static method which displays the total result of 3 user input results(the first and second are 15% and the third is 70%) but im getting an error due to what i think is
outputting a string which contains a double. Please help! Heres the code
HERES THE ERROR:
found : java.lang.String
required: java.lang.Double
return String.format("The answer is: %g",ans);
^
1 error
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5901
|
|
Hi, and welcome to the Ranch! A couple of things that will help you get the most out of your experience here:
1) UseCodeTags(←click) when posting code so it will be readable. I've added them for you this time.
2) TellTheDetails(←click). Copy/paste the exact, complete error message and indicate clearly which line is causing it.
|
 |
Paul Witten
Ranch Hand
Joined: Oct 10, 2012
Posts: 86
|
|
Tommy Cregan wrote:
public static Double calcOverallMark(WeightedAverage w){
double f = w.getResult1()/15*100;
double s = w.getResult2()/15*100;
double t = w.getResult3()/70*100;
WeightedAverage ans = new WeightedAverage(f,s,t);
return String.format("The total result is: %g",ans);
}[/code]
HERES THE ERROR:
found : java.lang.String
required: java.lang.Double
return String.format("The answer is: %g",ans);
^
So as the error message says, Tommy: "Required: java.lang.Double". Look at your return type in the method declaration above. It says Double (with a big D). What are you returning?
Another case where the compiler didn't lie!
|
 |
Paul Witten
Ranch Hand
Joined: Oct 10, 2012
Posts: 86
|
|
Paul Witten wrote:
So as the error message says, Tommy: "Required: java.lang.Double". Look at your return type in the method declaration above. It says Double (with a big D). What are you returning?
Another case where the compiler didn't lie!
|
 |
Paul Witten
Ranch Hand
Joined: Oct 10, 2012
Posts: 86
|
|
So as the error message says, Tommy: "Required: java.lang.Double". Look at your return type in the method declaration above. It says Double (with a big D). What are you returning?
Another case where the compiler didn't lie!
|
 |
 |
|
|
subject: Creating a static method for calculating averages
|
|
|