# Fibonacci Sequence final project

Printable View

• November 28th, 2012, 09:55 AM
Sylis
Fibonacci Sequence final project
I'm having some trouble with my program for the final project.

My assignment is to take a user input of how many digits of the fibonacci sequence they wish to display, and then....display them.
However when I first ran the code, it would throw an ArrayOutofBounds exception.

So say the user input 5, the system would print out the first three correctly but then give me the exception. Which I understand to be that it's calling reserved array spots that don't exist. I fixed the code by adding 2 to the user input which defines the array size, but I feel like there has to be a better way.

Code java:

```import javax.swing.*; public class Fibonacci { private static String strFibN; protected static int intFibN; //public static void UserInput() public static void main(String[] args) { strFibN = JOptionPane.showInputDialog(null, "Please Input the Amount of Numbers you Wish to Display of the Fibonacci Sequence"); intFibN = Integer.parseInt(strFibN); int[] Fib = new int[intFibN + 2];   Fib[0] = 1; Fib[1] = 1;   for(int inc=0; (inc)<intFibN; inc++) { Fib[(inc + 2 )] = Fib[inc] + Fib[(inc + 1)] ; System.out.println(Fib[inc]); } }   }```

The reason for the formula being + 2 is because I've already got the first two digits so the next array slot to be filled should be Fib[2] but I'm sure you all gathered that.

Also, I'd like the output to be in JOptionPane format, but I can't seem to get it to work without having to click ok, for every single array slot, which is just a pain. Any thoughts?
• November 28th, 2012, 10:08 AM
KevinWorkman
Re: Fibonacci Sequence final project
What array index are you trying to access? How many indexes does the array have?
• November 28th, 2012, 11:00 AM
Sylis
Re: Fibonacci Sequence final project
The amount of indexes are set by the user. If they want to display the first ten numbers of the sequence then the array would have ten indexes. And all of them need to be printed. Hope that answered you're question.
• November 28th, 2012, 11:18 AM
KevinWorkman
Re: Fibonacci Sequence final project
Not really- I mean specifically, when this error occurs, what index are you trying to access? How many indexes does the array have? Hardcode the user's input to help narrow it down. Step through it with a debugger, or at least add some print statements, to help figure out what's going on.
• November 28th, 2012, 12:56 PM
Sylis
Re: Fibonacci Sequence final project
So say the user inputs 5 for the amount of digits they wish to display of the fibonacci sequence it creates an array 0-4. This is what is displayed

1Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Fibonacci.main(Fibonacci.java:23)

1
2

I guess since I fixed it, I shouldn't really worry about it? But just adding two to the initial array size seemed like an inefficient way to handle things.
• November 28th, 2012, 01:10 PM
KevinWorkman
Re: Fibonacci Sequence final project
I'm not really sure why you're adding 2 to your variable inside the for loop. Hint: what index do you want to start at? What index do you want to end at?
• November 28th, 2012, 02:55 PM
Sylis
Re: Fibonacci Sequence final project
I'm adding 2 to the variable inside the for loop because I've already got index 0 and 1 filled with the first 2 digits of the sequence. And they're used in the equation. The next index that needs to be filled is Fib[2], so I added to in order to fill the Fib[2] slot while still being able to use the increment-er in the for loop. The ending index will be 1 less than the user input, this is because of the starting index being zero. So if the user inputs 10 the index would go from 0-9 which is 10 indexes. If it went from 0-10 that would be 11 indexes and incorrect.

Deleting the + 2 to the initial array size throws an exception, and deleting the + 2 from the variable in the array size of the formula makes my output:
2 1 0 0 0 0 0 0 0 0
Which isn't what it should be. It should be: 1 1 2 3 5 8 13, 21,....... going on for forever.
Which I can get by adding plus two in the array size of the formula.

I guess what I'm really saying is, is that the first calculation goes:
Fib[inc]+Fib[inc+1]=Fib[inc+2]
which is really saying
Fib[0]+Fib[1]=Fib[2] which is really 1+1=2
And then as it runs again it's Fib[1]+Fib[1+1]=Fib[1+2]
which is Fib[1]+Fib[2]=Fib[3]
1+2=3
And it goes in that pattern until it reaches the user input. so the next would be 2+3= 5, 3+5=8 and so on.

Sorry if it's all a little confusing. I really hope it doesn't sound like I'm talking to you like your dumb. Maybe I'm just still not understanding what your asking.

Calculus and Java :/

I think for the sake of less confusion I'm just going to leave the program as is and move on with my next question.

How do I get the JOption to print the entire array in one window vice clicking ok each time to display the new index?
• November 29th, 2012, 08:19 AM
KevinWorkman
Re: Fibonacci Sequence final project
My only point is that your +2 is probably in the wrong place. If you want to start at index 2, why does your for loop start at index 0?

As for your JOptionPane, anything you put inside a loop will happen multiple times. If you want it to only happen once, you have to take it out of the loop.
• November 30th, 2012, 03:59 PM
Sylis
Re: Fibonacci Sequence final project
Right, I did end up making a post at code ranch, and make a great deal of headway. I do however have another problem at the moment. I've added some exceptions and moved some stuff around in order to satisfy the requirements for the final project. I have an idea of what's wrong (I think) but can't seem to figure out what to do about it.
Code java:

```import java.util.Arrays; import javax.swing.JOptionPane;   public class UseFibonacci { protected static int[] Fib; protected static int iFibN; protected static String sFibN;   public static void main(String[] args) { CreateFibException Input; sFibN = JOptionPane.showInputDialog(null, "Please Input the Amount of Numbers you Wish to Display of the Fibonacci Sequence from 1-50: "); Input = validateFib(sFibN); Fib = new int[(iFibN)]; Fib[0] = 1; Fib[1] = 1; for(int inc = 0; (inc)<(iFibN - 2); inc++) { Fib[(inc + 2)] = Fib[inc] + Fib[(inc + 1)] ; } } public static CreateFibException validateFib(String sFibN) { int iFib = 0; CreateFibException objFib; try { iFibN = Integer.parseInt(sFibN); objFib = new CreateFibException(iFibN); JOptionPane.showMessageDialog(null, "The First " + iFibN + " Digits of the Fibonacci Sequence are \n" + Arrays.toString(Fib)); } catch(NumberFormatException numFormExc) { JOptionPane.showMessageDialog(null, "Letters and Symbols are not Excepted please Enter a Number."); // return null; } catch(FibException fibExc) { JOptionPane.showMessageDialog(null, fibExc.getMessage()); // return null; } return objFib; } }```

When I run the program it prints "The first (iFibN) numbers of the fibonacci sequence are null" clearly it's not supposed to be null, and I'm assuming that it's doing this because of the way I have the try/catch returns set up? I'm not quite sure how to fix it though.

I do appreciate your help. and the advice you're giving is the same advice that I've gotten on another forum. And I believe I fixed it in the code above? I was also given the advice to not use "int" for the beginning of variable names and that it's bad form.
• November 30th, 2012, 04:20 PM
KevinWorkman
Re: Fibonacci Sequence final project
Since I don't know what help you're receiving elsewhere, I don't want to waste time repeating what you've already heard. It's considered polite to offer links to other places you've posted so we can avoid repeating stuff other people already told you.
• November 30th, 2012, 04:22 PM
Sylis
Re: Fibonacci Sequence final project