0

さて、x、y、z 座標のスプレッドシートがあります。x と y は整数で、z は float です。すべての座標を読み取りたいのですが、実行しようとするとエラーが発生します。これはエラーです:

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

1 に答える 1

0

あなたの答えは、スローされた例外にあります。esej がコメントで述べているように、(数値ではなく) 列名の最初の行を解析しています。したがって、入力文字列「XCoord」の NumberFormatException

解決策は、最初の行をスキップするか、データ ファイルから削除することです。

于 2013-03-02T22:24:47.607 に答える