# Array Problem

• March 5th, 2010, 05:57 PM
Cammack
Array Problem
Hello, first time poster here. Be gentle with me.

I'm having some trouble with a small program I'm writing for my Intro to Java course. Specifically the way my For loop is interacting with my array.

This program when finished would: ask the user how many bowling scores they wish to enter, store the entries in the array using the first for loop, using the second loop repeat the entries back to the user and then report a total and average.

I ran the debugger, which we haven't gone over yet and it seemed to think the problem existed on line 23. 23 being inside my first For loop.

Code :

import javax.swing.JOptionPane;

public class Main
{

public static void main(String[] args)
{
String sNumberOfScores = "";
int iNumberOfScores = 0;
int iIndex = 0;
double[] dScores;

dScores = new double[ iNumberOfScores ];

sNumberOfScores = JOptionPane.showInputDialog("How many scores do you wish to enter?");
iNumberOfScores = Integer.parseInt( sNumberOfScores );

for(iIndex = 0; iIndex < iNumberOfScores; iIndex++)
{
sInput = JOptionPane.showInputDialog("Please enter " +
"score # " + (iIndex + 1) + ";");
dScores[iIndex] = Double.parseDouble(sInput);
}

for(iIndex = 0; iIndex < iNumberOfScores; iIndex++)
{
System.out.println("The scores you entered were " +
dScores[iIndex]);
}

double dAverage = (dScores[iNumberOfScores])/iNumberOfScores;
double dTotal = (dScores[iNumberOfScores]);

System.out.println("Your total pin count is " + dTotal);
System.out.println("Your average score per game is " +
dAverage);

}
}

Running the program results in it asking me how many scores I want to enter, then asking for the first score. Upon entering the first score it always comes up with, Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Main.main(Main.java:23) I believe I failed to set the size of my array but to me it looks good.

I know my calculations for Average and Total are wrong as are most likely other things outside of my For loops but I can slam my head against my desk till those are fixed. I know its simple I just can't see to find what this thing wants.