Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Methods: parameters

 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a method:

//Method for testing which calculations to use and return the result
public String calc(String choice, double c, double f)

Now my understanding is the parameter is String.
That is, this method will return something of type String?

So why when I state:


I get the following error on compiling:

C:\java>javac CalcC2F.java
CalcC2F.java:61: incompatible types
found : void
required: java.lang.String
return System.out.println ("When the temperature in celcius is: "
+c +" The temperature in fahrenheit is: "+f);
^
CalcC2F.java:66: incompatible types
found : void
required: java.lang.String
return System.out.println("Sorry, Invalid Data! ");
^
2 errors
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your return is type String, therefore you should construct the String and return it. e.g.

What you are returning is the "standard" output stream ,hence incompatible types.
 
Dave Cryder
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The function System.out.println() does not return a value (has a type of "void"). Try this:
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another quick point, why are you not converting 0.0 celcius into farenheit or vice versa?
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks people - your response is much appreciated!

Re: Nigel Brown's response

Another quick point, why are you not converting 0.0 celcius into farenheit or vice versa?


Good point. As 0.0 C is 32 F. Mmmm - I'm not sure. I will think about this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic