-1

次のプログラムを実行すると、エラーが発生します。

Exception in thread "main" java.lang.NumberFormatException: For input string: "[-52.06907251530153, -90.45811476077165]"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at code.readFile(code.java:31)
    at code.main(code.java:22)

このエラーはどういう意味ですか? そして、なぜこれが起こっているのですか?

import java.io.*;
import java.util.Arrays;

public class code
{
  public static void main (String [] args) throws IOException
  {

    int points = 10000, dimension = 2, lengthA = 100;//int variables are declared and initialized

    PrintWriter fileOut = new PrintWriter (new FileWriter ("arrayPoints.txt"));

    double length [] = new double [dimension];//array for length is declared
    double coordinate [][] = new double [points][dimension];//coordinate array is declared

    writeTofile(length, coordinate, points, dimension, lengthA);//calls writeTofile method

    for (int i = 0; i < points; i++){
      fileOut.println(Arrays.toString(coordinate[i]));//converts array to string and prints to file
                      }//end for
      fileOut.close();//writes to file
      readFile();// calls readFile method

  }//end main method
  public static void readFile() throws IOException
  {
    BufferedReader readFile = new BufferedReader (new FileReader ("arrayPoints.txt"));

    int readTimes [] = new int [10000];
    for(int i = 0; i < 10000; i++){
    readTimes [i] = Integer.parseInt(readFile.readLine());
    System.out.print(readTimes [i]);
    }//end for

  }

    public static void writeTofile (double length[], double coordinate[][], int points, int dimension, int lengthA)
    {
      for(int z = 0; z < dimension; z++){//fills the length array with the the set value of lengthA
      length[z] = lengthA;
      }//end for

    for(int x = 0; x < points; x++){//runs 1000 times to print 1000 data points
      for (int y = 0; y < dimension; y++){//runs 2 times to print an x and y coordinate
        coordinate [x][y]= (2 *Math.random() - 1) * length[y];// finds a random number in the range and assiigns it to the coordinate array
      }//end for
    }//end for
  }//writeTofile method
}//code
4

3 に答える 3

0

[37.34351500606841, -30.837458930508465]ではないのでInteger
ここで起こるようです:Integer.parseInt(readFile.readLine());

于 2013-09-24T00:19:04.320 に答える
0

それらが整数であり、それらで実行されていると仮定して、ファイルから一連の行を読み取っていInteger.parseIntます。これらのすべての行に整数が含まれているわけではないため、失敗します。

各行を読み取って整数として解析する代わりに、これらの非整数部分で使用する前に、を使用string.split()して各行を分割することをお勧めします。string.substring()Double.parseDouble

于 2013-09-24T00:19:57.077 に答える
0

あなたが投稿したスタック トレース スニップでは、「37.34351500606841」は整数ではありません。不完全なスタック トレースから正確に何が起こっているのかを判断するのは困難ですが、その値で Integer.parseInt を呼び出すと、NumberFormatException が発生します。

于 2013-09-24T00:21:24.983 に答える