This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes outputing an arraylist of arraylists to a file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "outputing an arraylist of arraylists to a file." Watch "outputing an arraylist of arraylists to a file." New topic
Author

outputing an arraylist of arraylists to a file.

Michael Comerford
Greenhorn

Joined: Jan 16, 2010
Posts: 13
Hi there,

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:

1,2,3,4,5,
21,45,54,32,67,
Alan,Steve,Rick,Jon,Chris,
1.82,1.53,1.72,1.49,1.93,

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.

Any pointers?


Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

The example you posted isnt using the PrintWriter's print method.

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Michael Comerford
Greenhorn

Joined: Jan 16, 2010
Posts: 13
Hi Hunter,

sorry I described the one using PrintWriter in my post and then posted the System.out one! here it is:

this just generates a blank file


Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Where you call System.out.println() in your last line, replace 'System.out' with q

Hunter
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Don't forget to flush and close your resources.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Michael Comerford
Greenhorn

Joined: Jan 16, 2010
Posts: 13
thanks for your help guys, I'm now trying to format my output so that it is in columns rather than rows.

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?

Wim Vanni
Ranch Hand

Joined: Apr 06, 2011
Posts: 96

Maybe adding the variables amountOfRows and amountOfCols like I did here, helps you see where you went wrong:



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!
Wim
Wim Vanni
Ranch Hand

Joined: Apr 06, 2011
Posts: 96

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)
at arraylistplaySUL2.main(arraylistplaySUL2.java:105)


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.

Wim
Michael Comerford
Greenhorn

Joined: Jan 16, 2010
Posts: 13
Wim thanks that was a really useful explanation, I fixed the problem and the code works now.

Michael
 
Consider Paul's rocket mass heater.
 
subject: outputing an arraylist of arraylists to a file.