how to read a text delimited file using 2 dimentional array in java ??
hi,
I am new to java programming.. I have to do a task where in i have to read a text delimeted file in an array.. For example.. If the file is as follows
Name place Value
adi goa 20
shri mumbai 30
riya bangalr 45
I want it to be read in java so as to get an array[row][columns]
This is something i am currently upto, but cant get any further.
import java.io.BufferedReader;
import java.io.FileReader;
public class generateGML{
public static void main(String[] argv)
throws Exception{
BufferedReader fh = new BufferedReader(new FileReader("miRTarbase.txt"));
String s;
while ((s=fh.readLine())!=null){
String[] columns = s.split("\t");
String name = columns[0];
String place = columns[1];
String value = columns[2];
It reads columns,But I want it two dimentionally,as in something like matrix[row_num][column_num].
Can anyone please suggest me..
Re: how to read a text delimited file using 2 dimentional array in java ??
I have moved this post to a more appropriate forum.
First off, are you sure you want to use a 2D array for this? It seems like you want to use a one-dimensional array (or List) that contains Objects that hold the name, place, value information.
But if you want to use a 2D array, just think of it as an array of arrays. You get an array from the split method, so you just have to add that array to another array holding those results. Does that make sense?
Re: how to read a text delimited file using 2 dimentional array in java ??
well yes i get what you are saying and it does make sense.. :) so i have found something like
File input = new File(argv[0]);
BufferedReader br = new BufferedReader(new FileReader(input));
String line;
List<List<String>> rows = new ArrayList<List<String>>();
while ((line = br.readLine()) != null) {
String[] columns = line.split("\t");
List<String> columnList = Arrays.asList(columns);
rows.add(columnList);
}
but the last like here "rows.add(columnList) g
ives me an error.
And also that i am a little confused with the list of list.. Is it that the 1st list separates columns and the second list separates rows ? and also that what is the "row" and "columnList" doing here ?? It would be easy for me if i get a small explanation for the above code as to how it works.. Thank you in advance.. :)