错误java.lang.NumberFormatException:对于输入字符串:"&引用;
我正在读这个文件错误java.lang.NumberFormatException:对于输入字符串:"&引用;,java,string,exception,integer,numberformatexception,Java,String,Exception,Integer,Numberformatexception,我正在读这个文件 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
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
但当我读取文件时,程序会抛出以下异常:
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)
我检查了我的代码并进行了调试,但找不到问题
我使用了一个数组来保存列之间的所有空格(我只读取前四列),然后是一个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]);
}
}
publicstaticvoidmain(字符串[]args)引发异常{
文件f=新文件(“C101.txt”);
FileReader fr=新的FileReader(f);
BufferedReader读取器=新的BufferedReader(fr);
字符串nada=reader.readLine();//C101
nada=reader.readLine();//espacio en blanco
nada=reader.readLine();//车辆=25
nada=reader.readLine();//数量,容量
nada=reader.readLine();//25200
nada=reader.readLine();//espacio en blanco
nada=reader.readLine();//客户
nada=reader.readLine();//encabezados
nada=reader.readLine();//espacio en blanco
字符串[]espacios;
内部容量=200;
int custno、xcoord、ycoord、demand;
ArrayList guardar=新的ArrayList();
while(reader.ready()){
espacios=reader.readLine().trim().split(“”);
for(int i=0;i
很抱歉给您带来不便,谢谢您抽出时间。您必须确保
espacios[0]
到espacios[3]
实际上是整数。您可能应该做的是打印出来检查。如果其中一个不是整数,integer.parseInt()
将引发该异常。在单个空格上拆分,但整数列之间有多个空格
espacios = reader.readLine().trim().split("\\s+");
但所有的数字都是正确的Integers@ArCiGo:你要么使用去毛刺器,要么打印任何东西。把所有的假设都扔出窗外。答案是1+。我发现了错误:在.txt文件的末尾,作者留下了一个空白,很可能这解决了问题,但后来在阅读该文件时也导致了同样的问题