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.

Recommended Answers

All 3 Replies

I forgot to mention that my compiler points out line 21 as a mistake though I am not sure why.

What exactly is the error message the compiler (not the runtime) gives for that line? Is line 21 the System.out.println ?
It's absolutely pointless to try to execute a program that has compile errors. Always fix all the compile errors before trying to run the program.

at line 21 you misspelled the fib4() (lowercase f) method.
and fib4(i), i is long but it should be integer.
I hope this help

commented: You are right! +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.