This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Creating a static method for calculating averages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating a static method for calculating averages" Watch "Creating a static method for calculating averages" New topic
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: 6109
    
    6

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:
Tommy Cregan wrote:
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
Tommy Cregan wrote:
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!
 
GeeCON Prague 2014
 
subject: Creating a static method for calculating averages