I am trying to write a method that accepts two integer parameters and returns their average as a float, but for some reason it is only returning a double. I am quite sure that there needs to be "f" after the resulting value, but it's not showing up. Can someone please help me fix this?

public static float average(int x, int y)
	{
		return (x + y)/2;
	}
System.out.println("average(\"7, 5\") = " + Lab7.average(7, 5));

RESTULTS:
average("7, 5") = 6.0

return (float)((x+y)/2.0);

OR

return ((float)(x+y))/2.0;
return (float)((x+y)/2.0);

OR

return ((float)(x+y))/2.0;

I tried both suggestions but my results are still only double... is there some other way of converting an int to a float? I tried casting too, but it didn't work either. Perhaps I'm doing something completely unrelated wrong?

You're probably storing it in a double variable. For example

double result = 0.0;

result = average(x,y);
Now result is a double which is a larger type than float.

Are you expecting it to print the "f"? Doubles and floats don't look any different as output.

Are you expecting it to print the "f"? Doubles and floats don't look any different as output.

Well if you're right, then I suppose the assignment will be a lot easier to complete... but I'm quite sure my instructor said that the printed value of a float was supposed to be followed by an "f".

Then do System.out.println("f") afterwards. lol.

The literal value of a float in your code would be "6.0f", but printed output does not show that "f" unless you put it there explicitly.

Your output is perfectly fine.

The literal value of a float in your code would be "6.0f", but printed output does not show that "f" unless you put it there explicitly.

Your output is perfectly fine.

Ok I'm really glad that's the case, because I had spent hours trying to figure out what I was doing wrong lol. Thank you very much for your time!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.