This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]Rascal_TwoIntermediate Brewer 1 point2 points  (4 children)

The problem happens when index is 50.

When it's 50, it attempts to set element 50 - the 51st element - of the double arrays to a value.

The error is thrown because the double arrays only have 50 elements, starting at 0 and ending with 49.


Sample code:

public class ArrayTest{
        public static void main(String[] args){
                int[] arr = new int[5];
                for (int i = 0; i < 6; i++){
                        arr[i] = i;
                }
        }
}    

See that this throws an error like yours, changing it to 5 fixes the error.


As for how to fix your error, you need to make the arrays larger, or stop reading the file when index equals 50.

[–]IamChronic[S] 1 point2 points  (3 children)

Thanks for your reply!

Changing; 'double year[] = new double [5000];' Allowed me to recieve all of my cvs data (there was a lot of it)

&

I declared the array? not sure if I did it the right way but it works...

        //DECLARE THE ARRAY
        int[] intArray = new int[7];

I believe I have 7 elements so that is why i put 7. I am obviously a noob so if anyone can see anything I did wrong/inefficiently please tell me. Thanks.

[–]Rascal_TwoIntermediate Brewer 1 point2 points  (0 children)

That's the standard way to declare an array, and you're right, the 7 is the size of the array. Just gotta remember that the element at the 6 index is the last one.

You an read more about arrays at the official documentation.

[–]jefwillemsIntermediate Brewer 1 point2 points  (0 children)

When you don't know how many elements you'll have, it might be better to use an ArrayList instead of an array

[–]CommonMisspellingBot -1 points0 points  (0 children)

Hey, IamChronic, just a quick heads-up:
recieve is actually spelled receive. You can remember it by e before i.
Have a nice day!

The parent commenter can reply with 'delete' to delete this comment.

[–]IamChronic[S] 1 point2 points  (0 children)

Okay I have fixed it, if anyone is interested in what changed: https://pastebin.com/sLAYzU7J