File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes read a comma seperated file into  an arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "read a comma seperated file into  an arraylist" Watch "read a comma seperated file into  an arraylist" New topic
Author

read a comma seperated file into an arraylist

bharathwaj vijayakumar
Greenhorn

Joined: Apr 12, 2010
Posts: 2
Hi everybody

I have wrriten a code to save data from a text file into any array list. I am using array list for the first time. I have used only array before. In the text file I have data that is seperated by comma , I need each element to be stored in a seperate array position that is .....

input: 1,10,0,0,3,2
2,14,0,0,3,3
.
.
.
in the above line number one should be stored in array position 1,1......number 10 should be stored in array position 1,2...like a two dimensional array.....here is my code...



here is my output

[ 1,10,0,0,3,2, 2,14,0,0,3,3, 3,9,0,0,2,1, 4,15,0,0,2,1, 5,11,0,0,2,3, 6,12,0,0,3,5, 7,15,0,0,3,5, 8,11,0,0,2,2, 9,15,0,0,3,4, 10,14,0,0,2,4]

10

but I need it to be like the following:

1 10 0 0 3 2
2 14 0 0 3 3
.
.
.
.
I just need a idea of how to do it.........
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Iterate over the ArrayList, printing each element on its own line.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bert Wilkinson
Ranch Hand

Joined: Oct 28, 2009
Posts: 33
Well, I think you have a couple problems that you've brought up:

1. your treating your list as "line separated values" not comma separated...because you are treating each line as a string data element. If that is truly what you want to do (which is doubtful) and all you want to do is print it out like that, do as Rob suggested and just iterate over the array with a for loop and print each value on a new line.

2. If your numbers are truly independent values, your structure needs to change. You can't make a 2-dimensional array list, so your options are either:

a. capture each value individually, and use modular arithmetic on the ArrayList to index into the correct position or for printing.

b. use nested ArrayLists as in:



You probably want to do something like (b.) above. In that example, you have an array of patients....each patient has an array of "data values". And you're treating your list of numbers as independent values instead of as a String.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Maybe better to declare the List by its interface:

List<List><Integer>> list = new ArrayList<List><Integer>>();
bharathwaj vijayakumar
Greenhorn

Joined: Apr 12, 2010
Posts: 2
thank you very much....I will go with the idea of doing nested array list....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: read a comma seperated file into an arraylist