【问题标题】:NumberFormatException program errorNumberFormatException 程序错误
【发布时间】:2013-09-24 00:16:26
【问题描述】:

当我运行以下程序时,我得到了错误:

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

【问题讨论】:

  • 堆栈跟踪的其余部分是什么(您发布的错误行之后的行)?
  • 这是完整的:java.lang.NumberFormatException:对于输入字符串:“[37.34351500606841, -30.837458930508465]”在 java.lang.NumberFormatException.forInputString(Unknown Source)at java.lang.Integer .parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at J5_3_MultiDimArray.readFile(J5_3_MultiDimArray.java:42) at J5_3_MultiDimArray.main(J5_3_MultiDimArray.java:33) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native方法)在 sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)在 sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)

标签: java bufferedreader numberformatexception


【解决方案1】:

您正在从文件中读取一堆行,假设它们是整数并在它们上运行Integer.parseInt。它失败了,因为并非所有这些行都包含整数。

您可能希望尝试使用string.split()string.substring() 拆分每一行,然后再对这些非整数部分使用Double.parseDouble,而不是读取每一行并将其解析为整数。

【讨论】:

  • 我不熟悉 string.split() 和 string.substring() 这是如何工作的?它看起来怎么样?
  • This string tutorial 应该可以帮到你。
【解决方案2】:

因为[37.34351500606841, -30.837458930508465] 不是Integer
它似乎发生在这里:Integer.parseInt(readFile.readLine());

【讨论】:

    【解决方案3】:

    在您发布的堆栈跟踪片段中,“37.34351500606841”不是整数。从不完整的堆栈跟踪中很难判断到底发生了什么,但是,如果您在该值上调用 Integer.parseInt,您将得到 NumberFormatException。

    【讨论】:

    • 不,他正在使用 readln() 读取完整的行,然后尝试使用 Integer.parseInt() 对其进行解析,因此不会自行评估前导左括号。此外,Integer.parseInt 会在任何非整数值上抛出 NFE。
    猜你喜欢
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 2019-08-29
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多