java从文件加载数据并根据第一行的长度插入数组
文件中的第一行有年份,这些年份的数量表示表的大小。例如,我有2015年、2016年、2017年,然后表格将存储三个数字。变量的值可以是1到年数。 如果年数少于年数,则剩余的年数根据最后一个给定值确定。 我的问题是,我不知道如何将这些数字添加到表中
例如,文件
2015年2016年2017年2018年2019年
IMP 1.03 1.04 1.07
public class Main{
private int years;
private double [] IMP;
private StringTokenizer st;
private String text;
public void readFromFile(String fname){
try(BufferedReader br = new BufferedReader(new FileReader(fname))){
text = br.readLine();
st = new StringTokenizer(text);
years = st.countTokens() - 1;
IMP = new double[years];
text = br.readLine();
String split [] = text.split("[ \t]");
int i =0;
while(i<years){
IMP[i] = Double.parseDouble(split[1]); // here is my problem. I dont know how set data here
}catch (IOException e) {
e.printStackTrace();
}
}
}
输出应该是
2015年2016年2017年2018年2019年
IMP 1.03 1.04 1.07 1.07 1.07
但现在是
2015年2016年2017年2018年2019年
IMP 1.03 1.03 1.03 1.03 1.03 1.03
共 (0) 个答案