| | |
Fibonacci Recursion - Illegal Start of expression!
Thread Solved |
•
•
Join Date: Nov 2006
Posts: 3
Reputation:
Solved Threads: 0
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:
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:
Java Syntax (Toggle Plain Text)
import java.util.Scanner; public class Fibonacci { public static void main(String[] args) { int fib; int n; Scanner scan = new Scanner(System.in); System.out.println("input a number"); n = scan.nextInt(); public int calcFib (int n) { int ans; if (n<2) { ans = n; return ans; } else { ans = (n-1) + (n-2); return ans; } } System.out.println(calcFib(n)); } }
I think the problem here is that you are writing a method inside of a method.
The should not be contained in the main method. Instead, form the class like this:
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.
The
Java Syntax (Toggle Plain Text)
public int calcFib(int n)
Java Syntax (Toggle Plain Text)
public class Fibonacci {<blockquote>public static void main(String[] args) {<blockquote>//Insert main method body here </blockquote>} public static int calcFib(int n) {<blockquote>//Insert recursive method here </blockquote>} </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.
![]() |
Similar Threads
- illegal start of expression (Java)
- JAVA Illegal Start of Expression Error (Java)
- illegal start of expression (Java)
- Why illegal start of expression error? (Java)
- illegal start of expression (Java)
Other Threads in the Java Forum
- Previous Thread: Help BubbleSort Array wont run?
- Next Thread: store table values into database which are updated dynamically on JSP page
| Thread Tools | Search this Thread |
actuate add android api applet application applications array arrays automation balls bank binary bluetooth business chat class clear client code codesnippet collections component database defaultmethod development dice digit dragging ebook eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health hql html hyper ide idea image infinite int integer invokingapacheantprogrammatically j2me java javame javaprojects jni jpanel julia linux list main map method methods mobile myregfun mysql netbeans nonstatic openjavafx parameter pearl php problem program project recursion repositories scanner scrollbar server set sms sort sorting spamblocker sql sqlserver state storm string sun superclass swing swt thread threads tree windows






