The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

This code gives me the error: missing return statement. I understand why that is (I think!), and I need to find a way around it, because what Ive been
trying to do is get it to do the calculation only if neither if statement is run. However, because It's in an if statement, public double doesn't seem to realise that I am trying to return something, but only inside an if statement.

Re: Returning a value with an if-statement

In your method if the value of displayValue is above 100 or below -273 it displays a message dialog and right after the user interacts with it, the control reaches end of the method and it searches for the return <double> statement which you've missed, therefore it eventually generates a compilation error.

Re: Returning a value with an if-statement

Yes, that was my conclusion too. I apologise, maybe I didn't make myself clear. What I was hoping for was someone to tell me what I would be able to do, to stop that compilation error, and at the same time, only run:

return displayValue = (9 * displayValue)/ 5 + 32;

if the user had entered a number in between the permitters I have allowed?

Re: Returning a value with an if-statement

You get the error because if the display value is > 100 or < -273, there is no return statement encountered, and all paths through your method must end by returning a double.

As it is, you could fix it by returning a double value after each of the error messages was displayed. However, the only value you could return after each message dialog would have to be some sort of special value indicating an error, which you'd have to check in the calling code. This is poor practice - return values should not be used to indicate errors that way. Luckily, Java explicitly provides a mechanism for dealing with this situation.

The correct way to handle a situation where an error or invalid value means a method can't complete correctly, is to throw an exception. This exception can be given information about the error, and the calling code can catch it and handle it appropriately. If you haven't yet looked at exceptions, you should do so. See The Java Tutorials : Exceptions.

If you're not permitted to use exceptions yet, the only sensible alternative is to take the two value checks out of that method and do them before calling the method, so it is guaranteed to receive a valid value. This does the job, but isn't an ideal solution, because it puts the onus on the calling code to check what it passes to your method, and a well-written method should generally check the arguments it has been given.