Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将字符串转换为Int时发生Java错误_Java - Fatal编程技术网

将字符串转换为Int时发生Java错误

将字符串转换为Int时发生Java错误,java,Java,使用以下代码将字符串转换为int时,我得到了错误 Exception in thread "main" java.lang.NumberFormatException: For input string: "null1" 下面是代码(错误发生时的代码行): 那么null1是什么意思呢?这不应该仅仅意味着1吗,因为null不意味着什么?还有,我怎样才能解决这个问题 谢谢问题在于standardProgramResult是null。您需要使用调试器来找出原因 我有两个建议不能直接回答您的问题,但可

使用以下代码将
字符串
转换为
int
时,我得到了错误

Exception in thread "main" java.lang.NumberFormatException: For input string: "null1"
下面是代码(错误发生时的代码行):

那么
null1
是什么意思呢?这不应该仅仅意味着1吗,因为null不意味着什么?还有,我怎样才能解决这个问题


谢谢

问题在于
standardProgramResult
null
。您需要使用调试器来找出原因

我有两个建议不能直接回答您的问题,但可以帮助您找出问题所在:

  • parseInt()
    的值指定给一个变量,以便可以根据需要多次重用结果:

    int result = Integer.parseInt(standardProgramResult);
    
    一般来说,您不应该在一行代码中执行太多操作,因为这会使跟踪错误变得更加困难

  • 当您有一个值数组并且需要对数组中的每个值重复相同的任务时,请使用for循环


  • 首先,您只能解析一次并使用循环:

    int programResult = Integer.parseInt(standardProgramResult);
    int numbProgram=0;
    for (int output: output){
      numProgram += Math.abs(programResult - output)
    }
    

    也就是说,
    standardProgramResult
    不包含整数值,因此无法解析。例外情况表明了这一点

    在代码中的某个地方,您可能有如下内容:

    standardProgramResult = someVar1 + someVar2;
    
    someVar1
    “null”

    要更好地理解和处理此异常,请使用异常:

    int programResult = 0;
    try {
        programResult = Integer.parseInt(standardProgramResult);
    } catch (NumberFormatException e) {
        System.err.println("programResult was not a number: " + programResult);
        // possibly ignore error, or terminate...
        // e.printStackTrace(); // prints the stack trace
        // throw e; // throws the error for someone else to handle
        // System.exit(1); // terminate indicating an error in execution
    }
    int numbProgram=0;
    for (int output: output){
        numProgram += Math.abs(programResult - output)
    }
    

    您确定它是
    null 1
    而不是
    null
    ?无论如何都要获得更好的帮助帖子和整个stacktrace。
    standardProgramResult
    的值是多少?为什么要调用
    parseInt()
    这么多次?你知道for循环是什么吗?standardProgramResult可能不是一个正确的整数-因此在试图解析它时会抛出一个错误。请包含定义
    standardProgramResult
    的代码。这意味着
    standardProgramResult
    “null1”
    它不是正确的整数,无法解析为
    int
    ,因此您将获得
    NumberFormatException
    。如果您希望
    standardProgramResult
    具有除
    null1
    之外的其他值,那么问题在于您没有向我们展示的代码,因此我们无法为您提供太多帮助。目前看来,它似乎是将
    null
    与数字字符串(在本例中为
    “1”
    )连接在一起的结果。或者,For循环可以写成
    For(int-output:output)
    。我不确定哪一个更好,不过…哦,发现了我的问题。我正在启动一个新进程,该进程的标准输出为空。我意识到这是因为你说我把变量加起来,其中一个是空的。Thanks@APCoding很高兴提供帮助,您应该添加异常处理,您可能希望忽略错误,或者以更礼貌的方式终止:)您的try-catch示例已损坏;
    NumberFormatException
    中的消息已经包含未分析的文本,而且
    Math.abs()
    不会引发
    NumberFormatException
    @dimo414 ahh谢谢,我是说。。。我举了一个例子,他可能根本不想打印错误。
    standardProgramResult
    不是
    null
    ,如果是,它将引发
    NullPointerException
    。它是字符串
    “null1”
    @dimo414,它没有提到抛出NPE。我确信我看到它为
    null
    输入抛出了
    NumberFormatException
    ,这肯定符合文档中的行为。假设
    “null 1”
    不是打字错误,那么
    字符串
    实际上不是
    null
    可能是正确的。是的,显然是这样。然而,
    “null1”
    显然不是一个打字错误,
    null
    的错误消息只是
    “null”
    @dimo414,因此意外添加一个无关的
    1
    ,可能会出现打字错误。否。OP帖子中的消息是输入字符串的
    :“null1”
    。这显然不同于
    null
    int programResult = 0;
    try {
        programResult = Integer.parseInt(standardProgramResult);
    } catch (NumberFormatException e) {
        System.err.println("programResult was not a number: " + programResult);
        // possibly ignore error, or terminate...
        // e.printStackTrace(); // prints the stack trace
        // throw e; // throws the error for someone else to handle
        // System.exit(1); // terminate indicating an error in execution
    }
    int numbProgram=0;
    for (int output: output){
        numProgram += Math.abs(programResult - output)
    }