简体   繁体   English

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

[英]How do I read these coordinates from a spreadsheet?

Okay so I have a spreadsheet of x, y, and z coordinates. 好的,我有一个x,y和z坐标的电子表格。 The x and y are integers, and the z is a float. x和y是整数,而z是浮点数。 I want to read all of the coordinates, but I'm getting an error when I try to run it. 我想读取所有坐标,但是尝试运行时出现错误。 This is the error: 这是错误:

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

And here is my code: 这是我的代码:

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.
    }

Your answer is right there in the exception that is thrown. 您的答案就在那里,除了抛出的异常。 As esej says in the comments, you are parsing a first row of column names (as opposed to numbers). 正如esej在评论中所说,您正在解析列名的第一行(而不是数字)。 Hence the NumberFormatException for input string "XCoord" 因此,输入字符串“ XCoord”的NumberFormatException

The solution is to skip the first row, or remove it from the data file. 解决方案是跳过第一行,或将其从数据文件中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM