简体   繁体   English

NumberFormatException:对于输入字符串:“”,在解析时

[英]NumberFormatException: For input string: “” while parsing

I wished to turn a string of single digit integers into an Integer ArrayList, and I'm getting a very odd exception. 我希望将一个整数位数的字符串转换为Integer ArrayList,但我遇到了一个非常奇怪的异常。 Here's the sample code: 这是示例代码:

    Scanner test = new Scanner(System.in);
    String[] temp = null;
    String line = null;
    ArrayList<Integer> artemp = new ArrayList<>();

    while(test.hasNext()) {
        line = test.next();
        temp = line.split("");
        for(int i = 0; i < temp.length; i++)
        artemp.add(Integer.parseInt(temp[i]));
        for(int i : artemp) System.out.println(i);

    }

It is basically supposed to read the string of ints from stdin, put them in an arraylist as primitives, and then print them out. 基本上应该从stdin读取int字符串,将它们作为原始变量放入arraylist中,然后打印出来。 Of course, this is just a small test case to which I narrowed down my larger error. 当然,这只是一个很小的测试用例,我在其中缩小了较大的错误。

The exception this raises is the following: 引发的异常如下:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:504)
    at java.lang.Integer.parseInt(Integer.java:527)

As far as I can see, the following should take place: 据我所知,应该发生以下情况:

  1. line takes in stdin line进入标准输入
  2. temp gets filled with each of the individual single-digit values (split("") splits after every character) temp被每个单个的数字值填充(split(“”)在每个字符后拆分)
  3. Each digit (stored as a String ) is parsed as an int and added to artemp 将每个数字(存储为String )解析为一个int并将其添加到artemp
  4. Print out each digit. 打印出每个数字。

What exactly is going wrong here? 这到底是怎么了? If I print out temp with the following code: 如果我用以下代码打印出temp

for(String s : temp) System.out.println(s);

Then the digits get printed successfully (as Strings), and I there are no extra characters. 然后数字成功打印(作为字符串),并且我没有多余的字符。 How exactly could parseInt(temp[i]) be getting applied to the string "" ? 如何将parseInt(temp[i])应用于字符串""

First of all I'm suspecting your split, 首先,我怀疑你会分裂,

  temp = line.split("");

If you want to split with space ,it should be 如果您想与空间分开,它应该是

  temp = line.split(" ");

And if you want with "" only,then here is the cause. 如果只想使用"" ,那么原因就在这里。

There is nothing to do with conversion. 与转换无关。

There are some empty strings in you array . array有一些空字符串。

So exception while converting empty string in to integer. 因此,将空字符串转换为整数时会出现异常。

here 这里

 artemp.add(Integer.parseInt(temp[i]));

What you can do is,check the data before parsing. 您可以做的是,在解析之前检查数据。

if(temp[i]!=null && !temp[i].isEmpty()){
          artemp.add(Integer.parseInt(temp[i]));
}

From The Docs parseInt 从文档parseInt

Throws:
    NumberFormatException - if the string does not contain a parsable integer.

So your Code artemp.add(Integer.parseInt(temp[i])); 因此您的代码artemp.add(Integer.parseInt(temp[i])); is some where encoutering a unparsable String. 是一些无法解析的字符串。

您的split(“”)为null,您应该编写split(“”)(带引号的双引号)

Rather than using split, I'd think using the string itself will be better. 与使用split相比,我认为使用字符串本身会更好。 Try this. 尝试这个。

Scanner test = new Scanner(System.in);
String[] temp = null;
String line = null;
ArrayList<Integer> artemp = new ArrayList<>();

while(test.hasNext()) {
    line = test.next();
    for(int i = 0; i < line.length; i++)
        artemp.add(Integer.parseInt(""+line.charAt(i));
    for(int i : artemp) System.out.println(i);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取NumberFormatException:对于输入字符串:“”,将输入解析为数组时 - Getting a NumberFormatException: For input string: “”, while parsing inputs into an array 解析浮点数时String的NumberFormatException - NumberFormatException for String while parsing float 解析JTextField输入时出现NumberFormatException - NumberFormatException on parsing JTextField input 解析int时出现NumberFormatException - NumberFormatException while parsing an int 解析数字时出现NumberFormatException - NumberFormatException while parsing a Number java.lang.NumberFormatException:对于输入字符串:“”解析字符串表达式 - java.lang.NumberFormatException: For input string: “” parsing string expression java.lang.numberformatexception 解析字符串输入数据库时 - java.lang.numberformatexception when parsing string to input into database 解析包含Long值的JSON字符串时出现NumberFormatException - NumberFormatException while parsing a JSON String containing Long value 将十六进制字符串解析为int值时发生意外的NumberFormatException - Unexpected NumberFormatException while parsing a hex string to an int value java.lang.NumberFormatException:对于输入字符串:&quot;&quot;,而不是将字符串转换为数字 - java.lang.NumberFormatException: For input string: "", while not converting a string into a number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM