Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Returning a value with an if-statement

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Returning a value with an if-statement

    Hey,

    So Im a real Java Beginner, so forgive me if this is a stupid question.

    I've been having trouble trying to get an if statement to return a value.

    I wonder if it is to do with the return type or Data type Im using.

    public double oF()
        {
             if (displayValue > 100) {
                JOptionPane.showMessageDialog(frame, 
                        "The Temperature scale Celcius does not allow numbers above 100, Please enter a number lower that 100",
                        "Error", 
                        JOptionPane.INFORMATION_MESSAGE);
            }
            else if (displayValue < -273) {
                JOptionPane.showMessageDialog(frame, 
                        "The Temperature scale Celcius does not allow numbers below -273, Please enter a number greater than -273",
                        "Error", 
                        JOptionPane.INFORMATION_MESSAGE);
            }
            else return displayValue = (9 * displayValue)/ 5 + 32;
        }

    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.

    Could anyone tell me what I'm doing wrong ?

    Thank you,

    Angus90


  2. #2
    Junior Member Mrc0d3r's Avatar
    Join Date
    Jun 2011
    Location
    TCP/IP Layer 3
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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?

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default 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.

  5. The Following User Says Thank You to dlorde For This Useful Post:

    Angus90 (July 28th, 2011)

  6. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Returning a value with an if-statement

    Ah. Ok then, thank you very much. I will read all about Exceptions.

Similar Threads

  1. Having trouble returning the right out put.
    By byako in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 26th, 2011, 11:49 PM
  2. Method returning 0 for everything
    By JJTierney in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2010, 08:51 PM
  3. If statement not returning anything
    By Jonathan_C in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 15th, 2010, 03:38 PM
  4. Returning Null
    By Woody619 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 22nd, 2010, 12:53 PM
  5. throwing and returning
    By chronoz13 in forum Exceptions
    Replies: 6
    Last Post: October 25th, 2009, 12:00 AM

Tags for this Thread