• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

read a comma seperated file into an arraylist

 
bharathwaj vijayakumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iterate over the ArrayList, printing each element on its own line.
 
Bert Wilkinson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe better to declare the List by its interface:

List<List><Integer>> list = new ArrayList<List><Integer>>();
 
bharathwaj vijayakumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much....I will go with the idea of doing nested array list....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic