【问题标题】:Exception in thread "main" java.lang.NumberFormatException: For input string: "" (I entered digits, but it seems to have read an empty string)线程“main”java.lang.NumberFormatException 中的异常:对于输入字符串:“”(我输入了数字,但它似乎读取了一个空字符串)
【发布时间】:2015-04-12 01:08:17
【问题描述】:

这部分有问题:

BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
String faaltu = cin.readLine();
String inp = cin.readLine();
String[] part = inp.split("\\s");

for(int k = 0; k < part.length; k++)
{
    System.out.println(part[k]);
}

obj.Smax = Integer.parseInt(part[0]);

我给出了以下输入:

2
4 12345
3 1234

完整代码如下:

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codejam
{
    Codejam(){};
    static Codejam obj = new Codejam();
    int totalStanding = 0;
    int T;//no of test cases
    int[] S;// no of people at each given shyness level
    boolean[] standing;
    int Smax;
    int total = 0, newInv = 0;

    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
        obj.T = Integer.parseInt(cin.readLine());
        for(int i = 0; i < obj.T; i++)
        {
            obj.populate();
            obj.update();
            while (obj.totalStanding < obj.total)
            {
                obj.newInv++;
                obj.S[0]++;
                obj.update();
            }
            System.out.println("Case #" + i + ": " + obj.newInv);
        }


    }

    public void update()
    {
        for(int i = 0;i < obj.S.length; i++)
        {
            if ((totalStanding >= i) && (obj.standing[i] == false) )
            {
                obj.totalStanding += obj.S[i];
                obj.standing[i] = true;
            }
        }
    }
    public void populate() throws IOException
    {
        BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
        String faaltu = cin.readLine();
        String inp = cin.readLine();
        String[] part = inp.split("\\s");

        for(int k = 0; k < part.length; k++)
        {
            System.out.println(part[k]);
        }

        obj.Smax = Integer.parseInt(part[0]);
        obj.S = new int[Smax + 1]; 
        obj.standing = new boolean[Smax + 1];
        for(int j = 0;j < part[1].length(); j++)
        {
            obj.S[j] = part[1].charAt(j) - '0';
            obj.total += S[j];

        }

    }

}

遇到异常

线程“主”java.lang.NumberFormatException 中的异常: 对于输入字符串:“”在 java.lang.NumberFormatException.forInputString(Unknown Source) 在 java.lang.Integer.parseInt(未知来源) 在 java.lang.Integer.parseInt(未知来源) 在 Codejam.populate(Codejam.java:57) 在 Codejam.main(Codejam.java:24)

请指出我哪里出错了。

【问题讨论】:

    标签: java exception numberformatexception


    【解决方案1】:

    错误很明显,您尝试将一个空字符串解析为一个引发异常的数字。

    我们不知道您的输入是什么,但 part[0] 是导致错误的空字符串:

    obj.Smax = Integer.parseInt(part[0]);
    

    【讨论】:

    • 我输入了:2 4 12345 3 1234
    • @gnsb 如果确实是收到的输入,那么您的拆分将起作用。请在解析之前打印inp 并告诉我输出。
    • 打印变量来测试程序让我意识到错误:我已经读过了文件的结尾非常感谢您的帮助:)
    【解决方案2】:

    在我看来,您试图从输入中读取太多行。

    我怀疑您正在阅读文件末尾。

    我建议您将 cin.readline () 作为参数传递给您的 populate() 方法,这样您就不必打开另一个阅读器。

    您也不应该如此频繁地转义您的拆分表达式,我认为它目前读取的是“反斜杠和 s”而不是“空格”。

    【讨论】:

    • 是的,就是这样。谢谢 :) 关于转义字符:如果您在字符串文字中使用转义构造,则必须在反斜杠前面加上另一个反斜杠才能编译字符串。 (取自link
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    相关资源
    最近更新 更多