我正在寻找一些从excel文件中读取内容的帮助。 这是代码,我希望它从.csv文件中读取 生成相同的结果。
我的错误显示
java.lang.NumberFormatException: For input string: "companycode"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at java.lang.Double.valueOf(Double.java:502)
at testgbt.TestGBT.main(TestGBT.java:40)
请提前帮助和谢谢!!
package testgbt;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.Vector;
import java.io.FileOutputStream;
import testgbt.BoostingTree;
import testgbt.BoostingTree.ResultFunction;
public class TestGBT {
public static void main(String[] args) {
String data_name = "C:\\Mihir\\NetBeans\\TestGBT\\data.csv";
Vector<Vector<Double>> x = new Vector<Vector<Double>>();
Vector<Double> y = new Vector<Double>();
try {
FileInputStream fis = new FileInputStream("C:\\Mihir\\NetBeans\\TestGBT\\data.csv");
// -------- load data from the file -------------
// get object of data input stream
BufferedReader b_reader = new BufferedReader(new
InputStreamReader(new FileInputStream(data_name))); // buffer
String read_line;
while ((read_line = b_reader.readLine()) != null) {
String[] str_list = read_line.trim().split(",");
Vector<Double> buffer_x = new Vector<Double>();
buffer_x.add(Double.valueOf(str_list[0]));
x.add(buffer_x);
y.add(Double.valueOf(str_list[1]));
}
b_reader.close();
// --------- learn a function of y=f(x) -------
BoostingTree gbt_ranker = new BoostingTree();
ResultFunction res_fun = gbt_ranker.gradient_boosting_tree(x, y);
// --------- save the curve fitting result y=f(x) ------
FileWriter file_writer = new FileWriter("C:\\Mihir\\NetBeans\\TestGBT\\result.txt");
FileOutputStream fos=new FileOutputStream("C:\\Mihir\\NetBeans\\TestGBT\\result.csv");
for (int i = 0; i < x.size(); i ++) {
file_writer.append(String.format("%f,%f,%f\n",
x.get(i).get(0),
res_fun.get_value(x.get(i)),
y.get(i)));
}
file_writer.close();
} catch (Exception err) {
err.printStackTrace();
System.exit(0);
}
}
}
答案 0 :(得分:1)
您的输入CSV是否有标题行?从输入数据中删除标题行或更改代码以跳过第一行。
您可以通过更改此行轻松跳过第一行:
String read_line;
到此:
String read_line = b_reader.readLine();
答案 1 :(得分:0)
看起来str_list [1]不是您要查找的数据。事实上,您似乎正在尝试将一个列标题转换为双精度。您可能只需要跳过标题。