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: recursion code

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default recursion code

    As I am very new to Java Programming, I'd like to make sure that this code is right!
    I was trying to make a program that computes the factorial of an input integer (args[0]) using a recursive method factorial():
    The following is my program

    public class FactorialDemo {
    public static void main(String[] args) {

    int k=Integer.parseInt(args[0]);
    System.out.println(k + " factorial is " + factorial(k));

    }
    static double factorial(int n) {
    if (n==1) return 1;
    else return n*factorial(n-1);

    }

    }



    I hope i have written it right, however when I complile and run, there is an Exception "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0". I tried changing static type to double and long. I also tried adding "import java.util*;" What difference would it make to this program? can someone please help, thanks.


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

    Default Re: recursion code

    when I complile and run, there is an Exception "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0"
    The array you are trying to index has no elements in it. The length is zero. The first element at 0 does not exist.
    You need to pass a commandline argument to your program
    the program should test if the array that is passed to the main method is empty and if so print out a message and either exit or set a default value for the rest of the code to use.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Location
    Pob. Vieja Batuan, Bohol
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: recursion code

    Initialize the length of your array first and use while statement other than if statement..there are a lot of errors of the program..
    I can fix it..let's do it in private if you want..
    email me on macabudbudcyril@yahoo.com..I am willing to help for free..

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

    Default Re: recursion code

    If this is a student assignment you should not be doing the OPs work.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Location
    Pob. Vieja Batuan, Bohol
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: recursion code

    I'm just trying to give more ideas to make it more interesting..peace..

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

    Default Re: recursion code

    .let's do it in private if you want..
    I was referring to that part of your post.
    Why not keep it all on the forum so everyone can benefit?

  7. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: recursion code

    Quote Originally Posted by Norm View Post
    The array you are trying to index has no elements in it. The length is zero. The first element at 0 does not exist.
    You need to pass a commandline argument to your program
    the program should test if the array that is passed to the main method is empty and if so print out a message and either exit or set a default value for the rest of the code to use.
    Thank you very much, I noticed it is very simple after this as I learned how to pass an argument through net beans properties bar!

  8. #8
    Banned
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: recursion code

    public class Factorial {
    public static void main (String[] args) {

    int theNum, theFact;

    System.out.println("This program computes the factorial " +
    "of a number.");
    System.out.print("Enter a number: ");
    theNum=Console.in.readInt();

    theFact = fact(theNum);

    System.out.println(theNum + "! = " + theFact + ".");
    }

  9. #9
    Banned
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: recursion code

    you not yet dicribe [n] value

Similar Threads

  1. Recursion Help
    By WeaKeN in forum Algorithms & Recursion
    Replies: 10
    Last Post: May 27th, 2011, 08:17 AM
  2. [SOLVED] Recursion help
    By Actinistia in forum Java Theory & Questions
    Replies: 3
    Last Post: March 21st, 2011, 12:26 PM
  3. Recursion
    By javapenguin in forum Algorithms & Recursion
    Replies: 12
    Last Post: October 18th, 2010, 03:42 PM
  4. Recursion Help
    By vmr in forum Algorithms & Recursion
    Replies: 3
    Last Post: April 1st, 2010, 11:27 PM
  5. Recursion help
    By rhoruns in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 8th, 2010, 11:50 PM