As you are asking for a detailed explanation I will do my best. From what I can understand of your question you have a method methodA(float f1, float f2)
(2 floats as input) and you want to return a String? (Correct me if this is not what you meant).
The "problem" here is that you are using floats (numbers) as input and you want to return text (simply explained, keep in mind that floats are not exact numbers).
Java has a build-in method to do that, called toString
.
Your code would look like this I guess:
public String methodA(float f1, float f2) {
return Float.toString(f1) + " " + Float.toString(f2);
}
To assign the result to the String variable, simply do
public String str;
str = methodA(float f1, float f2)
Now, in short what happened:
- your
methodA
receives two floats as input. It converts the two floats to Strings by the Float.toString(...)
method and returns them. I added spaces between the two numbers for clarity (" "
, you can delete this or add more)
- Create a String variable
str
and call the method on it. The variable now contains the two floats that are converted to a String.
- Now do with it what you want.
You said you were a beginner, so I gave a thorough explanation. Hope it helps.