Hi, I'm new to Java. Bumped into an error when doing a program. The code below will basically explain everything. This error

java.lang.ArrayIndexOutOfBoundsException: 15
        at randomintegers.main(randomintegers.java:43)

keeps on showing up and the program will not execute further. If anyone can help me soon it would be great!
Here is my program below

import java.io.*;
import java.awt.*;
import hsa.Console;
import java.text.*;
import java.math.*;
import java.util.Random;

public class randomintegers
*******************************************************************************
    * The Random Integers Program will store 15 randomly generated integers between
    * 1 and 100 in an array. It will find the sum, average, highest value and lowest
    * value of the numbers. It will then display the sum, average, highest and
    * lowest number on the screen in sentence form and then display all numbers on
    * the screen.
    *******************************************************************************
    */

{
    static Console c;           // The output console

    public static void main (String[] args) throws IOException
    {
        c = new Console ();

        int x, maxvalue, sum;
        double average;
        int n[] = new int [15];
        Random randomint = new Random ();
        sum = 0;

        c.println ("Random Integers Program\n\nWelcome to the Random Integers Program. This program will randomly generate 15 integers. Their sum, average, their highest and lowest value will be displayed. Then, the 15 integers will be displayed.");
        
        

        for (x = 1 ; x <= 15 ; x++)
        {
            n [x] = randomint.nextInt (100);
        }
        

        for (x = 1 ; x <= 15 ; x++)
        {
            sum = sum + n [x];
        }
        c.println ("The sum of the 15 randomly integers are " + sum);

        average = sum / 15;
        c.println ("The average of the 15 randomly integers are " + average);

        int mxm, min;

        mxm = n [0];
        for (x = 1 ; x <= 15 ; x++)
        {
            if (n [x] > mxm)
            {
                mxm = n [x];
            }
        }
        c.println (mxm);

        min = n [0];
        for (x = 1 ; x <= 15 ; x++)
        {
            if (n [x] < mxm)
            {
                min = n [x];
            }
        }
        c.println (min);

        c.println(n[x]);
    }
}

Recommended Answers

All 2 Replies

if you declare an array with 15 cells the max index you shoud refer is 14 because the firt cell have the index 0;
so try this:

for(int i=0;i<15; i++)
......

hope it helps

commented: Thanks! Got it working now. +0

Thanks! Got it working now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.