错误 java.lang.NumberFormatException:对于输入字符串:“”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21517967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:06:39  来源:igfitidea点击:

Error java.lang.NumberFormatException: For input string: ""

javastringexceptionintegernumberformatexception

提问by ArCiGo

I'm trying to read this file

我正在尝试读取此文件

C101

VEHICLE
NUMBER     CAPACITY
  25         200

CUSTOMER
CUST NO.  XCOORD.   YCOORD.    DEMAND   READY TIME  DUE DATE   SERVICE   TIME

    0      40         50          0          0       1236          0   
    1      45         68         10        912        967         90   
    2      45         70         30        825        870         90   
    3      42         66         10         65        146         90   
    4      42         68         10        727        782         90   
    5      42         65         10         15         67         90   
    6      40         69         20        621        702         90   
    7      40         66         20        170        225         90   
    8      38         68         20        255        324         90   

But when I read the file, the program throws me this exception:

但是当我读取文件时,程序抛出了这个异常:

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 VRP.main(VRP.java:43)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

I cheked my code and debugged, but I can't find the problem.

我检查了我的代码并进行了调试,但我找不到问题所在。

I used an array to save all the spaces between the columns (I only read the first four columns) and then an ArrayList but it doesn't worked.

我使用了一个数组来保存列之间的所有空格(我只读取了前四列),然后是一个 ArrayList 但它不起作用。

    public static void main(String[] args) throws Exception {

    File f = new File("C101.txt");
    FileReader fr = new FileReader(f);
    BufferedReader reader = new BufferedReader(fr);

    String nada = reader.readLine();  //C101
    nada = reader.readLine();         //espacio en blanco
    nada = reader.readLine();         //vehicle=25
    nada = reader.readLine();         //number, capacity
    nada = reader.readLine();         //25, 200
    nada = reader.readLine();         //espacio en blanco
    nada = reader.readLine();         //customer
    nada = reader.readLine();         //encabezados
    nada = reader.readLine();         //espacio en blanco

    String[] espacios;
    int capacity = 200;
    int custno, xcoord, ycoord, demand;

    ArrayList<String> guardar = new ArrayList<String>();

    while (reader.ready()) {
        espacios = reader.readLine().trim().split(" ");
        for (int i = 0; i < espacios.length; i++) {
            guardar.add(espacios[i]);
        }
        custno = Integer.parseInt(espacios[0]);
        xcoord = Integer.parseInt(espacios[1]);
        ycoord = Integer.parseInt(espacios[2]);
        demand = Integer.parseInt(espacios[3]);
    }
}

Sorry for the inconvenient and thank you for your time.

很抱歉给您带来不便,感谢您抽出宝贵时间。

回答by La-comadreja

You got to make sure that espacios[0]through espacios[3]are actually integers. What you should probably do is print them out to check. If one of them isn't an integer, Integer.parseInt()will throw that exception.

你必须确保espacios[0]通过espacios[3]实际上是整数。您可能应该做的是将它们打印出来进行检查。如果其中一个不是整数,Integer.parseInt()将抛出该异常。

回答by Reimeus

You're splitting on a single space but there is more than one space between the integer columns

您在一个空格上拆分,但整数列之间有多个空格

espacios = reader.readLine().trim().split("\s+");