0
public static double [] loadGravityData( ) throws IOException
{
  File fileName = new File("gravity1.txt");
  Scanner inFile = new Scanner(fileName);

  double [] gravityData = {};
  while(inFile.hasNext())
   {    

   for(int i = 0; i <= gravityData.length; i++)
    {
     gravityData[i] = inFile.nextDouble();

    }

   }  
   inFile.close();

return gravityData;
}

The gravityData of i part is what is returning the exception. Any help would be much appreciated.

harley
  • 1

2 Answers2

1

You should initialize the array to be as long as the amounts of items you wish to read, and change

for(int i = 0; i <= gravityData.length; i++)
...

to

for(int i = 0; i < gravityData.length; i++)
...

This is because an array of length n has elements at 0,1,...,n-1.

If you count them you can tell that there are in fact n elements.

Edit, thanks Trengot

If you don't know how many items there are to read in advance, I suggest you look at ArrayList, which is a generic dynamic array type

nadavge
  • 561
  • 1
  • 3
  • 12
0

where are you allocating the array? In Java, arrays are of a fixed length and they do not auto-expand. In your case, you should use an ArrayList instead.

Additionally, you should change the <= to <

amahfouz
  • 1,972
  • 13
  • 17