I got a code from a book on Fibonacci written in java. And because it's still raw, I did some editing to it (change some lined of code, add code that enables input etc). But, it doesn't work. The other three fibonacci programs (with different algorithm) work fine, but not to this one. I am still very new to Java (but I learnt C once). Maybe if someone can point out the mistake, I'll very glad.
package fibonacci4;
import java.util.Scanner;
public class Fibonacci4 {
public static long fib4(int n){
return fiboHelp(0,1,n);
}
public static long fiboHelp(long x, long y, int n){
if (n == 0)
return x;
else if (n == 1)
return y;
else
return fiboHelp(y, x+y, n-1);
}
public static void main(String[] args) {
System.out.print("Input a number: ");
Scanner input = new Scanner(System.in);
long N = input.nextInt();
long startTime = System.nanoTime();
for (long i = 1; i<= N; i++)
System.out.println(i + ": " + Fib4(i));
long runningTime = System.nanoTime() - startTime;
System.out.println("Running time : " + runningTime + " ns");
}
}
My compiler (Netbeans) says something like this after I input a number as requested.
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at fibonacci4.Fibonacci4.main(Fibonacci4.java:21)
Java Result: 1.
What does it mean? I am still new so I cannot analyze things. But thanks in advance.