开发者

Java read a text file 2 columns and store in array

I have a text file such as "01/01/2011,14.25" "02/01/2011,16.78" and many more rows.I would like to read and store in an array first column as string(to be plotted later on x axe)and second column as double.Columns separator could be comma,semicolon,space or tab.I have been able to read single column text file using FileReader and BufferedReader,but don't know how to do if I have two or m开发者_如何转开发ore columns.I use Java and NetBeans.Thanks!


Read each line using buffered reader and then do that:

HashMap<String, Double> values = new HashMap<String, Double>;

// read the line here
String line = ...;
String strings[] = line.split(",");
values.put(strings[0], Double.valueOf(strings[1]);


1 Read whole row using BufferedReader readLine() method/
2 split String by your delimiter and have string array

for example

String str = "01/01/2011,14.25";
String arr[] = str.split(",");
//arr[0]; will hold date part and arr[1] will hold double part you can parst it to double using Double.parseDouble(string);
  • How to read file line by line
  • String javadoc
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜