• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating a static method for calculating averages

 
Tommy Cregan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic