Fibonacci Recursion - Illegal Start of expression!

Thread Solved

Join Date: Nov 2006
Posts: 3
Reputation: Crono is an unknown quantity at this point 
Solved Threads: 0
Crono Crono is offline Offline
Newbie Poster

Fibonacci Recursion - Illegal Start of expression!

 
0
  #1
Nov 18th, 2006
I am writing a program that calculates Fibonacci numbers... But I keep getting an error on my code when I try to compile it...:

Fibonacci.java:18: illegal start of expression
public int calcFib (int n) {
^
1 error


I don't know what I am doing wrong? Someone help? Here's my code:


  1.  
  2. import java.util.Scanner;
  3. public class Fibonacci {
  4. public static void main(String[] args) {
  5. int fib;
  6. int n;
  7. Scanner scan = new Scanner(System.in);
  8. System.out.println("input a number");
  9. n = scan.nextInt();
  10.  
  11. public int calcFib (int n) {
  12. int ans;
  13. if (n<2) {
  14. ans = n;
  15. return ans;
  16. }
  17. else {
  18. ans = (n-1) + (n-2);
  19. return ans;
  20. }
  21. }
  22. System.out.println(calcFib(n));
  23. }
  24. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Fibonacci Recursion - Illegal Start of expression!

 
0
  #2
Nov 19th, 2006
public int calcFib (int n) {
^


Could it be the word public perhaps?
Last edited by iamthwee; Nov 19th, 2006 at 9:38 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 262
Reputation: Dukane is an unknown quantity at this point 
Solved Threads: 22
Dukane's Avatar
Dukane Dukane is offline Offline
Posting Whiz in Training

Re: Fibonacci Recursion - Illegal Start of expression!

 
0
  #3
Nov 19th, 2006
I think the problem here is that you are writing a method inside of a method.

The
  1. public int calcFib(int n)
should not be contained in the main method. Instead, form the class like this:

  1. public class Fibonacci {<blockquote>public static void main(String[] args) {<blockquote>//Insert main method body here
  2. </blockquote>}
  3. public static int calcFib(int n) {<blockquote>//Insert recursive method here
  4. </blockquote>}
  5. </blockquote>}

Notice how I changed the method declaration to include static. You'll need to do that since you're not instantiating an object from the Fibonacci class, you're calling it in the "static context" from your static main method.

After making those changes to your class, I was able to compile it and run it.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 3
Reputation: Crono is an unknown quantity at this point 
Solved Threads: 0
Crono Crono is offline Offline
Newbie Poster

Re: Fibonacci Recursion - Illegal Start of expression!

 
0
  #4
Nov 22nd, 2006
I tried your suggestion and it worked. I did not notice that it was outside the class. Thanks for the help!
Last edited by Crono; Nov 22nd, 2006 at 6:22 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC