如何从电子表格中读取这些坐标?

时间:2013-03-02 20:24:30

标签: java spreadsheet

好的,我有一个x,y和z坐标的电子表格。 x和y是整数,z是浮点数。我想读取所有坐标,但是当我尝试运行它时出现错误。这是错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "XCoord"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source

这是我的代码:

BufferedReader br = new BufferedReader(new FileReader("./data/graphXYZ.csv"));
    String dataRow = br.readLine(); // Read first line.

    // The while checks to see if the data is null. If 
    // it is, we've hit the end of the file. If not, 
    // process the data.
    int i = 0;
    int xCoord, yCoord;
    float zCoord;

    while (dataRow != null) {
        String[] dataArray = dataRow.split(",");
        xCoord = Integer.parseInt(dataArray[0]);
        yCoord = Integer.parseInt(dataArray[1]);
        zCoord = Float.parseFloat(dataArray[2]);
        for(String item:dataArray) {
            System.out.print(xCoord + "\t");
            System.out.print(yCoord + "\t");
            System.out.print(zCoord + "\t");
        }
        System.out.println(); // Print the data line.
        dataRow = br.readLine(); // Read next line of data.
    }

1 个答案:

答案 0 :(得分:0)

你的答案就在抛出的异常中。正如esej在评论中所说,您正在解析第一行列名(而不是数字)。因此输入字符串“XCoord”

的NumberFormatException

解决方案是跳过第一行,或将其从数据文件中删除。