Results 1 to 3 of 3
  1. #1
    Southpaw is offline Member
    Join Date
    Dec 2012
    Posts
    24
    Rep Power
    0

    Default Problem with Java Code.

    This code has the correct input up till i input the fibonacci number 5. When i input 5 it comes out with 13 when it should be 8. I've tried everything i know to do but i cant figure out whats wrong. I'm trying to make the code work in a way that if the user was to input a number in the fibonacci sequence the program would return the next number in the sequence. Like if the user inputted 5 the program would return 8.

    Java Code:
    public class Fibonacci
    	{
    	int Fib (int n)
    		{
    			int in1=1,in2=1;
    			int sum=0;//initial value
    			int index=1;
    			while (index<=n)
    			{
    			sum = in1 + in2;
    			in1 = in2;
    			in2 = sum;
    			index++;
    			// sum=the sum of 2 values;
    			// in1 gets in2
    			// in2 gets sum
    			// increment index
    			}
    			return sum;
    		}
    	}
    This is the coding used for input and output.

    Java Code:
    import javax.swing.JOptionPane;
    
    public class FibonacciJDialog
    {
    
        public static void main(String[] args)
        {
    	 	String num;
    		int n;
    
    		num =
              JOptionPane.showInputDialog("Enter a number: ");
              n = Integer.parseInt(num);
    
    		Fibonacci box = new Fibonacci();
    
            JOptionPane.showMessageDialog(null,"That Fibonacci Number is " + box.Fib(n));
           }
    
    
    }

    Thank you for any help.

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

    Default Re: Problem with Java Code.

    You need to check your while-loop statement to see how many times the loop was executed. And then fix the loop condition so that it is looping not more that what it is needed. I believe this error is called as OBOB (Off-By-One Bug).

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: Problem with Java Code.

    Southpaw, I pointed you to the Forum Rules in an earlier thread.
    Java Assignment Problem.

    I'm closing this. Feel free to start a new thread with a subject line that is in compliance with the rules.

    This is a last warning. Any more threads with inane subject lines and you will be banned for a period.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  2. Java Code running problem
    By socboy6579 in forum New To Java
    Replies: 11
    Last Post: 10-26-2010, 04:07 AM
  3. Problem with simple code in Java ME SDK 3.0
    By marvas in forum CLDC and MIDP
    Replies: 0
    Last Post: 10-13-2010, 06:58 PM
  4. Replies: 6
    Last Post: 08-20-2010, 02:13 PM
  5. problem while running java code..
    By Jjava in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 06:33 AM

Tags for this Thread

Posting Permissions

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