Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
import java.util.*;
class Recursion {
    public static int fib(int n)
    {
        if (n <= 1)
            return n;
        return fib(n - 1) + fib(n - 2);
    }
    public static String reverse(String s)
    {
        if ((s==null)||(s.length() <= 1)){
           return s;
    }
    return reverse(s.substring(1)) + s.charAt(0);
    }
    public static boolean isPalindrome (String s)
    {
        if(s.length()==0 || s.length()==1)
            return true;
        
        if(s.charAt(0)==s.charAt(s.length()-1))
        return isPalindrome(s.substring(1,s.length()-1));
        return false;
    }
}
public class Main
{
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    System.out.println("Enter a value for n: ")
	    int n = sc.nextInt();
	    System.out.println("The n^th Fibonacci number is: ");
	    System.out.println(fib(n));
	}
}

What I have tried:

I'm not sure what's wrong but when I try to run my code it says Main.java:30: error: cannot find symbol
	    System.out.println(fib(n));
	                       ^
  symbol:   method fib(int)
  location: class Main
1 error
I'm a little confused as to why the variable isn't found although my method is declared. Please help!
Posted
Updated 8-Dec-22 3:25am

1 solution

You have two separate classes in your module: Recursion, which contains fib, and Main which contains the main method. You then try to call fib as if it is a member of the Main class, which it is not. It is a static method of the Recursion class. So you need to change the call in your main method to:
Java
System.out.println(Recursion.fib(n));

You will need to do the same with reverse and isPalindrome.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900