Fibonacci Sequence 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.

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?

So when you have a 5‑element array, and you try to display numbers when intFibN is equal to 5, what is the largest array index you will access? If you want to display 5 numbers, display 5. 1 1 2 3 5. If you go as far as fibN + 2 you will display 8 13 too. That looks like 7 numbers to me. Rather than using + 2 try - 1 and - 2 and start with index 2.

Don’t use nomenclature where the name of a variable starts with its type. That is called Hungarian Notation and should only be used in the version recommended by Joel Spolsky.
You can use i as a loop index for a simple for loop like yours.
Why are you using option panes for input? That is very old‑fashioned style. Why are you not using a Scanner or similar? The option pane only appeared once when I tried your code.
Don’t use tabs for indenting? Use spaces.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41070

29

posted Nov 28, 2012 13:37:12

0

If you want a long String to display in your option pane, use a StringBuilder, and don’t display it until after the loop has completed.

And yet another crossposter (link). What is in the last couple of days, crossposters are invading?!

The quieter you are, the more you are able to hear.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41070

29

posted Nov 28, 2012 15:52:35

0

Don’t know, but the crossposted site appears to be down. So it isn’t only us who have pantsdown days

Sean Heise
Greenhorn

Joined: Oct 30, 2012
Posts: 22

posted Nov 28, 2012 17:27:46

0

Sorry about the cross post, but it seems I get better results if I post in both places.
As you can see the other thread sort of turned into me explaining how my formula works, instead of me getting any real answers.

Sooo.... moving right along.

I appreciate all of your guys help. I figured it out, by Using Arrays.toString(Fib); instead of using a StringBuilder, I couldn't seem to quite figure out how to get a StringBuilder to work.
When I googled it, it looked like I would set up an array as:
StringBuilder[] Fib = new StringBuilder[intFibN];

But couldn't seem to figure out if that was supposed to replace the array I'm already using or what or how to implement it.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41070

29

posted Nov 29, 2012 02:39:22

0

Sean Heise wrote: . . . As you can see the other thread . . .

You mean as we didn’t see

When I googled it, it looked like I would set up an array as:
StringBuilder[] Fib = new StringBuilder[intFibN]; . . .

Please tell us where you found that info, so we can avoid that site!
What dreadful advice. You create one StringBuilder object, appending the numbers to it in each iteration of the loop. Then you can extract the entire text to display later. You will have to try out a few StringBuilder methods; it shouldn’t take long to find out how it works. Just beware of indices. If you find "text" at index i and try to find its next index, it will find the same index again. You have to move one place forward if you want to find a second occurrence.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41070

29

posted Nov 29, 2012 02:41:17

0

I have already told you how to sort out the out of bound exception, much faster than you got on the other site

Sean Heise
Greenhorn

Joined: Oct 30, 2012
Posts: 22

posted Nov 30, 2012 10:51:21

0

New Problems. I've changed the program around a little bit, in order to incorporate all of the stuff I need to to satisfy all of the parts for the final project.

So I've included some exceptions and a bit of inheritance in order to complete the rest of the final assignment objectives.

When I run the Code I get the proper output however the Array.toString(fib) prints "null" which isn't what it's supposed to print obviously it's supposed to print the sequence. I'm assuming it's because of the try catch returning the objFib and it being null? I know I haven't really fixed the rest of the stuff, but I figured if the program works, why mess with it? >.<

Sean Heise
Greenhorn

Joined: Oct 30, 2012
Posts: 22

posted Nov 30, 2012 10:58:53

0

I guess I should add that I can't figure out how to change the return objFib statement in order to get it to print the array.

Sean Heise wrote:...I figured if the program works, why mess with it? >.<

perhaps so that you understand and learn something about programming, rather than just "getting it to work"?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Sean Heise
Greenhorn

Joined: Oct 30, 2012
Posts: 22

posted Nov 30, 2012 11:52:29

0

I'm doing my best, but for only having been programming for three months I'm trying to do what I can.

Edit: Also, to add to this. I disagree that there's a wrong way or right way to program, my program isn't incorrect and still works. So saying that "learning something about programming" is subtracting two instead of adding two, is completely ludicrous.

Sean Heise
Greenhorn

Joined: Oct 30, 2012
Posts: 22

posted Nov 30, 2012 12:55:16

0

Campbell Ritchie wrote: - 2 and start with index 2.

I'm assuming you mean something liiiiike.

Edit: I should also note that for the above code the array is initialized as: int[] Fib = new int[iFibN]

Also as you can see above I fixed the way it displays the array without using a string builder.

Campbell Ritchie wrote:Please tell us where you found that info, so we can avoid that site!.

Found it here: Bad Advice? I don't know though, I may have misunderstand what exactly they were using or what the OP was actually asking.

Also, still having a problem with printing, the code is as above in the previous post with the try/catch statements. I'm assuming I should change the last return of the method to something else but I can't seem to figure out what it'll be.

Sean Heise wrote:I disagree that there's a wrong way or right way to program, my program isn't incorrect and still works. So saying that "learning something about programming" is subtracting two instead of adding two, is completely ludicrous.

You are certainly entitled to your opinion. I'll let others judge how much weight to put behind the opinion of someone with "three months experience".

There is certainly no one right way to program, but there are myriad ways to do it wrong. If you are trying to say that anything that works is fine programming, then you will be on the wrong side of the many seasoned developers who know otherwise.

This is perhaps one of most important, and hardest, lessons for novices to learn. "It works" isn't good enough.

There's no wrong way to learn, but there are assuredly wrong ways to program. You said yourself at the start of the thread that you felt there must be a better way. I suppose you might find one by trial and error, out of sheer luck, but the odds are against it. I think the best advice at this point is: StopCoding.

Wait, what? That can't be right, cause the program is so close to finished, can it? In truth, the more you code to a bad design, the harder it is to write code well. We make decisions throughout the design process that, for better or worse, we have to live with. In this case, unfortunately, it's worse.

It's not all bad, is it? No, it's not. But the bad decision is pretty much at the root of your problem. Just adding and subtracting integers here and there until we see what we want to see is no way to write a program.

But couldn't we just untangle the good code from the bad decision and go from there? Well we could, but often that takes even longer than just starting over with a better design. So: stop coding, stop thinking about Java, and think only about Fibonacci. You want the user to give you a number. You will return to them that number of elements in the Fibonacci series. Write down the steps to do that, in English, without a computer.

Sean Heise
Greenhorn

Joined: Oct 30, 2012
Posts: 22

posted Nov 30, 2012 14:31:32

0

I certainly understand that there are better ways to program than some ways.
It goes back to a question that was answered in class "If it's eligible to me; the program should be fine right?"
(I didn't ask this, someone else did).

I totally understand that this isn't correct. But for the sake of taking a java class I'm more trying to get things done rather than frustrate myself on something that works already.
I've spent plenty of time slamming my head against my keyboard trying to just get things to work, when I do I'm terrified of changing it for one, not being certain that I totally understand what someone is telling me.
And for two, that it's a triumph for me at the moment just to get the output correct. I at least recognize when the code I write is complete garbage, I had an instance of this several weeks ago.

But I suppose you're correct, the more messy code is the harder it is to fix. I guess my code making sense to me wasn't really the correct answer. I didn't really think it was that hard to read, but I guess being the one that wrote it, it wouldn't be for me.

Java hasn't really been the easiest thing for me to understand. Like Trigonometry it's totally abstract, there's tons of different ways to write the code and have the output be correct. But many more ways to write it and have it be a complete mess.

Programming hasn't been the easiest thing for me to accomplish. Which is one of the reasons I'm no longer pursuing a degree in it. However I do need a programming class up to advanced. So Java and advanced java for a minor I would like to obtain.

I'm just frustrated, and getting help hasn't been the easiest thing to accomplish, and it's definitely unhelpful when people even sound like they're trying to be discouraging. It also doesn't help that even my teacher isn't a java programmer and English isn't his first language, it's certainly a challenge to understand what's going on when it's difficult for the person to explain the concepts. Anyway, end rant. I do appreciate all the help I've received, there have been some that have been an extraordinary help in my class.

The website I asked about avoiding doesn’t come up. I shall try to edit your post, because I think there is a spelling error in the link.

There are people who take programming courses because they want to program and people who think they have to as a part of a larger course. But you might as well get the best mark you can.
You achieve two things by banging your head against a keyboard. A damaged computer and a headache.
If you stepped back and got a piece of paper and wrote something like this on it, you would see what I meant by starting at No 2.You have to put it in code tags on this website to get the numbers aligned. If you stepped back and worked out the algorithm, you could work out the workings of your array in a few minutes. And you could avoid getting annoyed with yourself and everybody else.
Fred, Bear and Dennis are right.
It is quicker to start from scratch, working out the algorithm, than to tinker with code which nearly works. I was very brief about the 2 and -2, and you ought to have asked for more explanation rather than guessing what I meant. You can guess a million times, and your code looks as if you are guessing. Within those million you will find something which works. Or you can work it out, which means paper pencil and eraser (the latter being the most important). And get it right first time. And get elegant code.
When I say paper and pencil I mean paper and pencil. Turn your PC off. You can turn it on when you have worked out the algorithm and see it work

Sean Heise
Greenhorn

Joined: Oct 30, 2012
Posts: 22

posted Nov 30, 2012 15:00:25

0

Haha, yes indeed.
At least this has been the least frustrating of my assignments for this class.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41070

29

posted Nov 30, 2012 15:01:55

0

There was a spelling errror in that link. I have corrected it, and found the discussion was totally different. It was about somebody who had 4 separate Strings, so an array might be appropriate. I also found the discussion difficult to understand.

Sean Heise
Greenhorn

Joined: Oct 30, 2012
Posts: 22

posted Nov 30, 2012 15:12:03

0

Campbell Ritchie wrote:The website I asked about avoiding doesn’t come up. I shall try to edit your post, because I think there is a spelling error in the link.

There are people who take programming courses because they want to program and people who think they have to as a part of a larger course. But you might as well get the best mark you can.
You achieve two things by banging your head against a keyboard. A damaged computer and a headache.
If you stepped back and got a piece of paper and wrote something like this on it, you would see what I meant by starting at No 2.You have to put it in code tags on this website to get the numbers aligned. If you stepped back and worked out the algorithm, you could work out the workings of your array in a few minutes. And you could avoid getting annoyed with yourself and everybody else.
Fred, Bear and Dennis are right.
It is quicker to start from scratch, working out the algorithm, than to tinker with code which nearly works. I was very brief about the 2 and -2, and you ought to have asked for more explanation rather than guessing what I meant. You can guess a million times, and your code looks as if you are guessing. Within those million you will find something which works. Or you can work it out, which means paper pencil and eraser (the latter being the most important). And get it right first time. And get elegant code.
When I say paper and pencil I mean paper and pencil. Turn your PC off. You can turn it on when you have worked out the algorithm and see it work

So I mean I don't really understand what you mean by starting at index 2. I mean I could set the counter to 2 and that way the first index would be the second index, but then I would have to add to to iFibN in order for the for loop to work.

Sean Heise
Greenhorn

Joined: Oct 30, 2012
Posts: 22

posted Nov 30, 2012 15:31:46

0

Popped I could declare a new variable set to two and then do like Fib[var++]?