I was told to use a while loop and call the nextNumber method until the input matches the corresponding fibonacci number, i.e. an input of 4 would give fib(4)=3. I'm getting fib(4)=1. Here is my code:

/**
   Class used to generate a Fibonacci number with a given input
*/
    public class FibonacciGenerator
   {
   /**
      Construct a FibonacciGenerator object to
         generate a Fibonacci number
   */
       public FibonacciGenerator()
      {
         fold1 = 1;
         fold2 = 1;
         fnew = 1;
         count = 0;
      }
   
   /**
      Method used to calculate a fibonacci number
      @return fnew the fibonacci number
   */
       public int nextNumber()
      {
         if(count < 2)
         {
         	count++;
            return 1;
         }
         else
         {
            fnew = fold1 + fold2;
         
            fold2 = fold1;
            fold1 = fnew;
         
            return fnew;
         }
      }
   
      private int fold1;
      private int fold2;
      private int count;
      private int fnew;
   }
import javax.swing.JOptionPane;
public class FibonacciGeneratorTest   
{ 

 public static void main(String[] args)
   {

    FibonacciGenerator fib;
    String fibonacci;

    int n;  
  
  while (USER DOESN'T CANCEL)
  {
    fib = new FibonacciGenerator(); 
    fibonacci  = JOptionPane.showInputDialog("Enter a Number or click Cancel to exit:"); 

    n = Integer.parseInt(fibonacci);

Any suggestions?

you could show us how you're calling nextNumber
starting from line 13 in that testclass, not really compilable, is it :)

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.