Still pretty new at this but I've created an arraylist of arraylists as a 2d data structure for demographic data.
What I'm ultimately trying to do is write the data as csv
In the code below I can output the data to with System.out and it looks right in the console - it prints:
I thought I could just use the PrintWriter q I've created and use q.print( j.next() + ",") instead of the System.out line, when I try to run it I get a NoSuchElementException.
The following code gives an IndexOutOfBoundsException at line 100 after printing the format I want for the first entry
I've had a look through the documentation for this exception and don't see how to resolve it, any clues?
I think it's good for you to play around with Java and ArrayList's like that. A few suggestions:
- catch a good book or website (possibly a page on the Java FAQ here too about it) about Java and read about collections and specifically about iterators
- consider looking into an existing API like CSV4J or even Apache POI (just 2 examples) to do all the work for you
- if the application grows it might be good to consider putting the data in a database of some sorts instead of a csv
Good luck with your project!
Michael Comerford wrote:... an IndexOutOfBoundsException ... I've had a look through the documentation for this exception and don't see how to resolve it, any clues?
An exception like this
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
should be read in the following way:
You're trying to access an element (of an ArrayList for example) at index 4 while the size of the ArrayList is only 4. Remember that this is 0-based so the 4 elements are at indexes 0, 1, 2 and 3. Hence the 'index out of bounds'; you're trying to access something that doesn't exist.