Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6734867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:56:25+00:00 2026-05-26T10:56:25+00:00

This is a continuation from: Recursive Fibonacci memoization . I keep getting an java.lang.ArrayIndexOutOfBoundsException

  • 0

This is a continuation from:
Recursive Fibonacci memoization.

I keep getting
an java.lang.ArrayIndexOutOfBoundsException error when trying to run the code. I’m getting the error on lines 63 and 40 which are

63: int fib = dictionary[num]; // dictionary is a class level array.
40: answer = fibonacci(num); // Answer is an int calling the fibonacci function and passing "num" to it.

Here is the full code:

import javax.swing.JOptionPane;

public class question2
{

//class variable
static int count = 0;
static int [] dictionary;


//main method
public static void main(String[] args)
{

//user input
int num = Integer.parseInt(JOptionPane.showInputDialog("Enter num:")), answer;

//Catches negative numbers, exits
if (num < 0) 
{
    JOptionPane.showMessageDialog(null,"ERROR: fibonacci sequence not defined for negative   numbers.");
    System.exit(1);
 }

//info dialog
JOptionPane.showMessageDialog(null,"About to calculate fibonacci("+num+")");

//giving the array "num" elements
dictionary = new int [num];

//fill array with 0
for (int i=0; i<dictionary.length;i++)
dictionary[i]=0;

//adds value of 1 for fib(1)    
if (dictionary.length>=2)
dictionary[1]= 1;

//method call
answer = fibonacci(num);

//output
JOptionPane.showMessageDialog(null,"Fibonacci("+num+") is "+answer+" (took "+count+" calls)");
}


  static int fibonacci(int num)
  {
count++;

// Base cases: f(0) is 0, f(1) is 1
if (num==0)
return 0;

if (num==1)
return 1;

// Other cases: f(num) = f(num-1) + f(num-2)/
else 
{

  //check array for value
  int fib = dictionary[num];

  //add new value to array
  if (fib==0) 
  {
    fib = fibonacci(num-1) + fibonacci(num-2);
    dictionary[num] = fib;
  }
  return fib;
}

  }


} //class terminator
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:56:25+00:00Added an answer on May 26, 2026 at 10:56 am

    The array is of size num (int fib = dictionary[num];) so the max index you can access is num-1. You try to access index num (dictionary[num] = fib;) which is out of bounds.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get this error when I run my code. This is a continuation from
In a continuation from this question . I'm trying to bind a given function
This is a continuation question from Opencv video frame giving me an error I
Ok - this is in continuation from my earlier question about sending an email
This is a continuation question from a previous question I have asked I now
This is a continuation of the post How does one access a method from
This is a continuation from a previous stackoverflow question. I've renamed some variables so
A continuation from this question I need a SQL statement that returns the number
this is a continuation from a previous JQueryUI Autocomplete question, I asked . This
This is a continuation from Redirect only HTML files? How can I change my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.