Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Fibonacci Sequence final project

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Fibonacci Sequence final project

    What array index are you trying to access? How many indexes does the array have?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    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.

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Fibonacci Sequence final project

    I apologize. Fibonacci Sequence final project (Beginning Java forum at JavaRanch)

Similar Threads

  1. Summing n terms of a Fibonacci Sequence
    By wayfaring_stranger in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 19th, 2012, 12:45 AM
  2. Recursive Fibonacci sequence optimization
    By aesguitar in forum Algorithms & Recursion
    Replies: 2
    Last Post: June 24th, 2012, 08:49 AM
  3. [SOLVED] Adding the "final" keyword makes difference, I'm confused with the sequence flow.
    By SmokyBrain in forum Object Oriented Programming
    Replies: 2
    Last Post: April 30th, 2012, 01:50 AM
  4. The Fibonacci sequence
    By Ryuk_93 in forum Android Development
    Replies: 1
    Last Post: March 26th, 2012, 11:56 AM
  5. final project idea
    By zulqar in forum AWT / Java Swing
    Replies: 4
    Last Post: November 3rd, 2010, 07:28 AM