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 4 of 4

Thread: If statement issue

  1. #1
    Junior Member phlegm's Avatar
    Join Date
    Nov 2013
    Location
    Rhode Island
    Posts
    12
    My Mood
    Relaxed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default If statement issue

    For some reason, when I create an if statement in my public double price() class, it gives me an error which says: This method must return a result of type double

     public double price()   
     {   
       double price = diameter;
       if(price>=6 && price<=10)
       {
         price = 5.25;
         return price;
       }
     }

    I just want to know how I'd be able to use an if statement in this class, because it won't let me.
    - phlegm


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: If statement issue

    The compiler wants the method to return a value for all cases when the if condition is true AND when it is false. The posted code does not return a value when the if condition is false. Add a return statement.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    phlegm (December 15th, 2013)

  4. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: If statement issue

    Giving a method and a variable the same name can only lead to confusion.
    Improving the world one idiot at a time!

  5. #4
    Junior Member phlegm's Avatar
    Join Date
    Nov 2013
    Location
    Rhode Island
    Posts
    12
    My Mood
    Relaxed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: If statement issue

    Quote Originally Posted by Norm View Post
    The compiler wants the method to return a value for all cases when the if condition is true AND when it is false. The posted code does not return a value when the if condition is false. Add a return statement.
    Aha! Thanks Norm. You've helped me multiple times, I really appreciate your dedication to this great forum.

    --- Update ---

    Quote Originally Posted by Junky View Post
    Giving a method and a variable the same name can only lead to confusion.
    Agreed, I'll keep that in mind. Although seeing how small my code is, it isn't that big of a deal
    - phlegm

Similar Threads

  1. Application performance issue with System.out.println(); statement
    By jawaharl0207 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 21st, 2013, 01:05 PM
  2. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  3. Conditional Statement Issue *Newbie Help*
    By dotChristo in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 7th, 2013, 03:25 PM
  4. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  5. [SOLVED] if statement issue
    By Elementality in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 17th, 2011, 03:29 PM