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: Recursion help

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Recursion help

    Hi everyone, so I'm still trying to figure out how to get my recursive function to work to calculate my sine so I figured I'd start with a factorial function and work my way up, adding the necessary parts. I looked at http://www.javaprogrammingforums.com...-concepts.html but I can't get any of the code to work.

    I get these errors
    ftest.java:1: class, interface, or enum expected
    public static int interativeFactorial(int n)
                  ^
    ftest.java:4: class, interface, or enum expected
            for(int i = 1; i < n; i++)
            ^
    ftest.java:4: class, interface, or enum expected
            for(int i = 1; i < n; i++)
                           ^
    ftest.java:4: class, interface, or enum expected
            for(int i = 1; i < n; i++)
                                  ^
    ftest.java:7: class, interface, or enum expected
            }
            ^
    ftest.java:9: class, interface, or enum expected
    }
    ^
    6 errors

    All I'm using is

    public static int interativeFactorial(int n)
    {
            int answer = 1;
            for(int i = 1; i < n; i++)
            {
                    answer *= i;
            }
            return answer;
    }
    }

    I've tried adding in
    public class ftest
    {
    public static int interativeFactorial(int n)
    {
            int answer = 1;
            for(int i = 1; i < n; i++)
            {
                    answer *= i;
            }
            return answer;
    }
    }

    but then I get the error

    Exception in thread "main" java.lang.NoSuchMethodError: main

    so then I tried putting in
    public class ftest
    {
    public static void main(String args[])
    {
    public static int interativeFactorial(int n)
    {
            int answer = 1;
            for(int i = 1; i < n; i++)
            {
                    answer *= i;
            }
            return answer;
    }
    }
    }

    then I just get more errors!
    ftest.java:5: illegal start of expression
    public static int interativeFactorial(int n)
    ^
    ftest.java:5: illegal start of expression
    public static int interativeFactorial(int n)
           ^
    ftest.java:5: ';' expected
    public static int interativeFactorial(int n)
                 ^
    ftest.java:5: '.class' expected
    public static int interativeFactorial(int n)
                                              ^
    ftest.java:5: ';' expected
    public static int interativeFactorial(int n)
                                               ^
    5 errors

    I'm pretty lost about what to do at this point if I can't even get this to compile and work right. Any clarification would be appreciated!


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Recursion help

    First off this is not recursion, you are using a loop, recursion relies on recalling the same method until some condition is met.

    Your code, although not recursion is fine, except you need a main method. All classes are a series of methods, main is the entry point of a program. You don't define a method in main, instead you call methods in main. Methods are created outside of the main method

    public class blah{
      public static void hello(){
        System.out.println("hello");
      }
      public static void main(String[] args){
        hello();
      }
    }

    Try compiling and runnning that and see if it helps you a bit with what you did wrong in your code.

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

    Actinistia (March 21st, 2011)

  4. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Recursion help

    This is an example of a program to recursively add two numbers.

    public class Add{
      public static int add(int x, int y){
        if(y == 0){
          return x;
        }
        else{
          return add(x + 1, y - 1);
        }
      }
      public static void main(String[] args){
        System.out.println(add(5, 6));
      }
    }

  5. #4
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: Recursion help

    Quote Originally Posted by sunde View Post
    First off this is not recursion, you are using a loop, recursion relies on recalling the same method until some condition is met.

    Your code, although not recursion is fine, except you need a main method. All classes are a series of methods, main is the entry point of a program. You don't define a method in main, instead you call methods in main. Methods are created outside of the main method

    public class blah{
      public static void hello(){
        System.out.println("hello");
      }
      public static void main(String[] args){
        hello();
      }
    }

    Try compiling and runnning that and see if it helps you a bit with what you did wrong in your code.
    Thanks for clarifying that, I stated that it was recursion based off the topic of the tutorial I found the code in. The example code was very helpful to show how recursion actually works. I think I understand why it didn't work now. I'm still pretty new to java and all, so thanks you for all your help!

Similar Threads

  1. Need help using Recursion in an assignment
    By hoiberg in forum Algorithms & Recursion
    Replies: 6
    Last Post: January 19th, 2011, 09:31 AM
  2. Recursion
    By javapenguin in forum Algorithms & Recursion
    Replies: 12
    Last Post: October 18th, 2010, 03:42 PM
  3. Recursion Help
    By vmr in forum Algorithms & Recursion
    Replies: 3
    Last Post: April 1st, 2010, 11:27 PM
  4. Recursion help
    By rhoruns in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 8th, 2010, 11:50 PM
  5. Problems with recursion
    By KingLane in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 20th, 2009, 11:02 PM