簡體   English   中英

如何從電子表格中讀取這些坐標?

[英]How do I read these coordinates from a 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.
    }

您的答案就在那里,除了拋出的異常。 正如esej在評論中所說,您正在解析列名的第一行(而不是數字)。 因此,輸入字符串“ XCoord”的NumberFormatException

解決方案是跳過第一行,或將其從數據文件中刪除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM