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

Thread: Can't get output HELP

  1. #1
    Junior Member moodycrab3's Avatar
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Can't get output HELP

    class keg
    {
    public static int fact(int n)
    {
    int factorial;
    if(n>0)
    {
    factorial=n*fact(n-1);
    return factorial;
    }
    else 
    if(n==0)
    return 1;
    }
    public static void main(String args[])
    {
    int result;
    int n=5;
    result=fact(n);
    System.out.println("The factorial of "+n+" is "+result); 
    }
    }

    I am a beginner in Java... When I compile the program it says "missing return statement" though i don't understand why.... HELP


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Can't get output HELP

    public static int fact(int n) {
    Is a method that returns an Integer value.
    You are returning values only if a condition holds.
    If that condition doesn't hold, then the method won't return anything.
    To correct this, you need to return a value which is suitable for a condition not holding.

    Off-topic: Could you please always surround your code with tags, such as ones in my signature.
    Last edited by newbie; February 5th, 2011 at 07:46 AM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Can't get output HELP

    public static int fact(int n)
    {
    int factorial;
    if(n>0)
    {
    factorial=n*fact(n-1);
    return factorial;
    }
    else
    if(n==0)
    return 1;
    }
    "missing return statement"
    because in the case when both of the conditions
    if(n>0)
    and
    else
    if(n==0)
    will false what this function will return.

    in java if you are specifying any return type then for every case it must return some value of that return type.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  4. #4
    Junior Member moodycrab3's Avatar
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Can't get output HELP

    Quote Originally Posted by DanBrown View Post
    because in the case when both of the conditions
    if(n>0)
    and
    else
    if(n==0)
    will false what this function will return.

    in java if you are specifying any return type then for every case it must return some value of that return type.
    Even if I add
    If(n<0)
    return 0;

    meaning even when all the possible conditions are addressed...... the output is still "return statement missing"
    I think the problem lies with the recursive...

  5. #5
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Can't get output HELP

    No.. it lies with how you're returning.
    If(n<0) is a condition itself...So thats not always going to be true.
    Place a return inside an else clause.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  6. #6
    Junior Member moodycrab3's Avatar
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Can't get output HELP

    Quote Originally Posted by newbie View Post
    No.. it lies with how you're returning.
    If(n<0) is a condition itself...So thats not always going to be true.
    Place a return inside an else clause.
    You mean for every If statement there has to be an else statement.... or else the logic doesn't work...

  7. #7
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Can't get output HELP

            if (condition1IsTrue) {
    //do something
            } else if (condition2IsTrue) {
    //do something else
            } else {
    //do something if NONE of the above conditions hold}
            }

    Currently, you're saying, if condition1 is true return x, else if condition2 is true, return y.
    But what you are not stating is, what to return if NONE of those conditions are true.
    The method always expects a value to be returned, and with your code only returning in conditional clauses, there is a risk that it might not always return a value.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  8. The Following User Says Thank You to newbie For This Useful Post:

    moodycrab3 (February 6th, 2011)

  9. #8
    Junior Member moodycrab3's Avatar
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Can't get output HELP

    I understand what you mean.... Thanks so very much...!!!

  10. #9
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Can't get output HELP

    Please mark this thread as solved.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. Output of 0 or 100?
    By Scotty in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 2nd, 2011, 05:15 PM
  2. what's the output?
    By dcshoecousa in forum Java Theory & Questions
    Replies: 3
    Last Post: November 27th, 2010, 11:11 AM
  3. Why am I getting this output?
    By ptabatt in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 6th, 2010, 07:36 PM
  4. need help with output.
    By VictorCampudoni in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 24th, 2010, 11:25 PM
  5. OutPut.
    By chronoz13 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 29th, 2009, 10:54 AM