3 Replies - 235 Views - Last Post: 24 June 2015 - 09:51 AM Rate Topic: -----

#1 p0ep0e  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-June 15

Exception error on Main - Array Out of Bounds

Posted 20 June 2015 - 01:02 PM

Hi! I am a Java beginner. I am doing the exercises in the Algorithms 4 text book for an online course. I am supposed to create a simple Fibonacci sequence using an array. I am getting the following error: "Exception in thread "main" - java.lang.ArrayIndexOutOfBoundsException: 0 - at fib.main(fib.java:9)". I've been working on making adjustments for more than an hour. I've tried watching videos, but I'm just not sure what the issue is. Any tips?! It'd be eternally grateful.

public class fib {

	public static void main(String args[]) {
		//Declaring fibNum
		int fibNum = 0;
		//Creating array
		int[] fibs =  new int[fibNum];
		//Starting Points for Fibs
		fibs[0] = 0;
		fibs[1] = 1;
		// System.out.println(F(100));
		//Begin calculation
		for(int N = 0; N < 100; N++){
			fibs[N] = fibs[N - 1] + fibs[N - 2];
		}
		//System.out.println(fibNum);
		
		for(int N = 0; N < 100; N++) {
			System.out.print(fibs[N] + " ");
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Exception error on Main - Array Out of Bounds

#2 p0ep0e  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-June 15

Re: Exception error on Main - Array Out of Bounds

Posted 20 June 2015 - 01:12 PM

Oh my goodness! I see what I did. I created a list from the bounds of zero! So I changed the code to what's below

public class fib {

	public static void main(String args[]) {
		//Declaring fibNum
		int fibNum = 100;
		//Creating array
		int[] fibs =  new int[fibNum];
		//Starting Points for Fibs
		fibs[0] = 0;
		fibs[1] = 1;
		// System.out.println(F(100));
		//Begin calculation
		for(int N = 0; N < 100; N++){
			fibs[N] = fibs[N - 1] + fibs[N - 2];
		}
		//System.out.println(fibNum);
		
		for(int N = 0; N < 100; N++) {
			System.out.print(fibs[N] + " ");
		}
	}
}



Oh! And then I realized I could just use the declaration for fibNum rather than manually setting the values. So sorry. I think I got it now.

public class fib {

	public static void main(String args[]) {
		//Declaring fibNum
		int fibNum = 100;
		//Creating array
		int[] fibs =  new int[fibNum];
		//Starting Points for Fibs
		fibs[0] = 0;
		fibs[1] = 1;
		// System.out.println(F(100));
		//Begin calculation
		for(int N = 0; N < 100; N++){
			fibs[N] = fibs[N - 1] + fibs[N - 2];
		}
		//System.out.println(fibNum);
		
		for(int N = 0; N < 100; N++) {
			System.out.print(fibs[N] + " ");
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 ndc85430  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 184
  • View blog
  • Posts: 716
  • Joined: 13-June 14

Re: Exception error on Main - Array Out of Bounds

Posted 22 June 2015 - 05:18 AM

That still isn't quite correct, is it? Look at line 14: what happens when N is 0?
Was This Post Helpful? 0
  • +
  • -

#4 manisha mulchandani  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 24-June 15

Re: Exception error on Main - Array Out of Bounds

Posted 24 June 2015 - 09:51 AM

I think you should start your loop from "2" because if you start it from 0 then at line 14

   fibs[N] = fibs[N - 1] + fibs[N - 2];



So, For N=0
fibs[0]=fibs[-1]+fibs[-2];


Set first two elements explicitly as follows:

fibs[0]=0;
fibs[1]=1;


And then start your loop from N=2 for calculation of Fibonacci series. :smile2:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1