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

  • SEARCH
  • Home
  • 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 8393999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:57:07+00:00 2026-06-09T19:57:07+00:00

I get the following error and I don’t know why. I tried looking it

  • 0

I get the following error and I don’t know why. I tried looking it up, but I didn’t find a solution.

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 1000000 at problem2.main(problem2.java:17)

Here is my code:

//Each new term in the Fibonacci sequence is generated by adding the previous two terms.
//By starting with 1 and 2, the first 10 terms will be:
//1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
//By considering the terms in the Fibonacci sequence whose values do not exceed four million, 
//find the sum of the even-valued terms.

public class problem2 {
    public static void main(String[] args) {
        int []a = new int[1000000];
        a[0] = 1;
        a[1] = 2;
        int sum=0;

        int i=2;
        while(a[i]<=4000000){
            a[i] = a[i-1] + a[i-2];
            i++;
        }

        for(int j=0;j<i;j++){
            sum = sum + a[j];
        }

        System.out.println("The sum is: " + sum);
        System.out.println("\nThere are " + i + " numbers in the sequence.\n");
        System.out.println("This are all the numbers in the sequence:");

        for(int j=0;j<i;j++){
            if(j+1==i){
                System.out.print(a[j] + ".");
                break;
            }

            System.out.print(a[j] + ", ");
        }
    }
}
  • 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-06-09T19:57:09+00:00Added an answer on June 9, 2026 at 7:57 pm

    The problem is not the size of the int[].

    Your while loop is constantly checking if a[i] is less than 4000000 while the i variable is already one index ahead. Each loop will have a[i] == 0.

    This change will fix the code for you:

    int i=1;
    while(a[i]<=4000000){
        i++;
        a[i] = a[i-1] + a[i-2];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried do gem1.8 install passenger but I get the following error ERROR: could
I really don't know, what the Problem is? I get the following error: File
In perl 5.8.5, if I do the following, I don't get an error: use
I tried the following code. Although I don't get any errors, it did not
I get following error when trying to access Xampp from a network I've tried
When compiling this code I get the following error : In function 'int main()':
I get the following error, when i run my script. fetch_scores.rb:20:in `<main>': undefined local
I get following error (SyntaxError): missing ] after element list when using eval function.
I get following error message, when I try to run git rebase -i for
I get following error when deploying on test server with II6 and Framework 3.5

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.