简体   繁体   English

java.lang.NumberFormatException用于将字符串转换为Java

[英]java.lang.NumberFormatException for String to Int Java

I have been searching for an answer to this, and I haven't been able to find someone who has rectified this quite yet. 我一直在寻找答案,但还没有找到可以解决这个问题的人。 I'm trying to populate an array, but check if it gets larger than a certain value. 我正在尝试填充数组,但是请检查它是否变得大于某个值。 However, when I run this, I get a Exception in thread "main" java.lang.NumberFormatException: For input string: "" Error. 但是,当我运行此命令时,在线程“主” java.lang.NumberFormatException中出现异常:对于输入字符串:“”错误。 Here is the code for it: 这是它的代码:

    public void setReadData() throws NoSuchElementException {
    try {

        for (int i = 0; i < getRows(); i++) {
            String numbers = p.nextLine();
            String[] inputs = numbers.split(" ");
            if (inputs.length > getCols()) {
                System.out.println("There are more columns than the size specified!\n");
                System.exit(1);
            }
            for (int j = 0; j < getCols(); j++) {
                int data = Integer.parseInt(inputs[j]);
                this.setData(i,j,data);
            }
        }

And the error code is: 错误代码是:

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)
at foo.Bar.setReadData(Matrix.java:49)
at foo.Bar.main(Matrix.java:226)

Any help is very much appreciated! 很感谢任何形式的帮助!

----EDIT---- - - 编辑 - -

To clarify, the old code that worked was: 澄清一下,起作用的旧代码是:

for (int i = 0; i < getRows(); i++) {
            p.nextLine();
            for (int j = 0; j < getCols(); j++) {
                this.setData(i,j,p.nextInt()); 
            }

        }

However, there is no checking that the whole line has been checked. 但是,不检查是否已检查整行。

Try this: 尝试这个:

for (int j = 0; j < getCols(); j++) {
    if(!inputs[j].equals("")){
        int data = Integer.parseInt(inputs[j]);
        this.setData(i,j,data);
    }
}
java.lang.NumberFormatException:

This exception invoke when you try to convert invalid string to integer or by passing null value to parseInt() method. 当您尝试将无效的字符串转换为整数或通过将null值传递给parseInt()方法时,将调用此异常。

  1. Invalid conversion 转换无效
 int var=Integer.parseInt("a");//throw NumberFormatException 
  1. Empty string 空字符串

int var=Integer.parseInt("");//throw NumberFormatException int var = Integer.parseInt(“”); //抛出NumberFormatException

If the line inputted contained two consecutive space characters, splitting on a space character would result in one if the elements being a blank, which would explain your error. 如果输入的行包含两个连续的空格字符,如果元素为空白,则在空格字符上分割将导致一个空格,这将解释您的错误。

To treat multiple spaces as one separator, do this: 要将多个空格视为一个分隔符,请执行以下操作:

String[] inputs = numbers.split(" +");

Adding the plus sign to the split regex will consume any extra spaces between numbers. 在拆分正则表达式上添加加号将消耗数字之间的所有多余空格。

So I ended up finding the answer. 所以我最终找到了答案。 I needed to add a p.nextline() above the for loop to get it to properly read the next line of code. 我需要在for循环上方添加p.nextline()才能使其正确读取下一行代码。 Thanks for all the help to everyone. 感谢大家的帮助。 Your answers were greatly appreciated. 非常感谢您的回答。

Apparently, what nextline does is it grabs the remainder of the current line, outputs that to whatever (if anything) you have it equal to, and move the pointer to the next line. 显然,nextline所做的是,它获取当前行的其余部分,将其输出到与之相等的任何内容(如果有),然后将指针移至下一行。 That's why I was having issues. 这就是为什么我遇到问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM