Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

outputing an arraylist of arraylists to a file.

 
Michael Comerford
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The example you posted isnt using the PrintWriter's print method.

Hunter
 
Michael Comerford
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where you call System.out.println() in your last line, replace 'System.out' with q

Hunter
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't forget to flush and close your resources.
 
Michael Comerford
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wim thanks that was a really useful explanation, I fixed the problem and the code works now.

Michael
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic