Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Array Problem

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

    import javax.swing.JOptionPane;
     
    public class Main
    {
     
    	public static void main(String[] args)
    	{
    		String sUserName, sInput;
                    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.

    Thanks in advance.
    Last edited by Cammack; March 5th, 2010 at 06:06 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Array Problem

    The problem is you allocate the array dScores before you set the variable iNumberOfScores. Even though iNumberofScores' value gets changed, the memory allocated for dScores doesn't. A simple shuffling around of code will fix the problem.

    And welcome

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Cammack (March 5th, 2010)

  4. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array Problem

    You sir are a genius. Not sure how I didn't see that.

    Thanks again.

Similar Threads

  1. [SOLVED] Array Problem
    By KevinGreen in forum Object Oriented Programming
    Replies: 2
    Last Post: January 24th, 2010, 09:07 PM
  2. array/string problem
    By RSYR in forum Collections and Generics
    Replies: 1
    Last Post: December 18th, 2009, 10:24 PM
  3. Problem with 2d array
    By Anyone in forum Collections and Generics
    Replies: 2
    Last Post: November 14th, 2009, 09:32 PM
  4. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 04:31 AM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM