Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2012
    Rep Power

    Default Fibonacci Number Calculator

    Hello, I have been learning Java for a while now and I have run into an roadblock with my code. What I am trying to do is run a Fibonacci Number Calculator while using a recursive method and not "for loopers".
    This is what I have so far.

    import java.util.*;
    public class Driver
    private int temp = 0;
    private Scanner in;

    public Driver()
    in = new Scanner(;
    public void getBase()
    System.out.println("Hello and welcome to the Fibonacci Number Calculator!!!");
    System.out.print("First off what number position do us to calculate the Fib Value of? ");
    temp = in.nextInt();
    public static void main(String[] args)
    FibonacciNumbers fnum = new FibonacciNumbers();
    public void displayNum()
    System.out.print("The fib num of " +temp);
    System.out.println(" is " +fnum.calcFibonacci(temp) );

    public class FibonacciNumbers

    public int calcFibonacci(int number)

    if ( ( number == 0 ) || ( number == 1 ) )
    return number;

    return calcFibonacci( number - 1 ) + calcFibonacci( number - 2 );



    The problem I am running into is that the variable I need to display the base also needs to be in the Driver which is "static" while also needing the Scanner in the same method because I need to run and return the value of the method in the other class.
    Any help is appreciated.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Bali, Indonesia
    Rep Power

    Default Re: Fibonacci Number Calculator

    I am not really understand what your question was. But what I can see in you code is that you cannot run it correctly. What you need to change in my opinion are:

    1. In the main() method you should create an instance of the Driver class.
    2. Call the getBase() method from this instance.
    3. Call the displayNum() method from this instance.
    4. Move the creation of the FibonacciNumbers instance into the displayNum() method.

    Other improvement that you can make is move the temp variable and the scanner into the getBase() method. Instead of returning void this method can return the number inputted by the user. And then pass this number to the displayNum() method by adding a parameter to this method. So you'll have new signature for this method like:

    Java Code:
    public int getBase() {
    public void displayNum(int number) {

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

Similar Threads

  1. Need Help - StackOverflowError - Fibonacci
    By ausglanville in forum New To Java
    Replies: 3
    Last Post: 04-13-2011, 04:43 AM
  2. Fibonacci Help
    By zit1343 in forum New To Java
    Replies: 16
    Last Post: 01-24-2011, 02:09 AM
  3. Fibonacci sequece
    By Bgreen7887 in forum New To Java
    Replies: 10
    Last Post: 12-03-2010, 08:27 PM
  4. Fibonacci sequence
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 6
    Last Post: 03-25-2010, 07:59 AM
  5. help with fibonacci
    By likemine in forum New To Java
    Replies: 8
    Last Post: 01-07-2010, 03:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts