Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException

I am a beginner to programming. I’m a few weeks into my first programming class, so please bear with me. I am not a person to ask for help, so I have searched for an answer extensively with no luck. This is also my first time posting anything in any type of forum, so if my question structure is off I’m sorry and I will correct for future posts.

This is the question I am tackling:

Write a program that declares an array “alpha” of 50 elements of type “double”. Initialize the array so that the first 25 elements are equal to the square of the index variable and the last 25 elements are equal to three times the index variable. Output the array so that 10 elements per line are printed.

This is what I got while running the program.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:50 on line 23 

And this is the code I’m working on

import java.util.*;
import java.lang.*;
public class ProgrammingProblem5_4
{
    public static void main(String[] args)
    { 
        int i=0;
        int count=0;
        double[] alpha = new double[50];


        if (i >= 25)
            alpha[i] = 3*i;
        System.out.print(alpha[i]+ " ");   
        count++;
        if (count==10) {
            System.out.println("/n");
            count=0;
        }
    }
}
}

Thank you in advance for any help. I’m not looking to have this done for me, I’m just stuck and need help finding my way.

Leave a Comment