gpt4 book ai didi

java - 输入数据 : java. lang.NumberFormatException:对于输入字符串: "2 "

转载 作者:行者123 更新时间:2023-11-30 08:54:42 26 4
gpt4 key购买 nike

这是我从文本文件中得到的数据:

21/08/12#ESE-6329#PV/5732#30
27/08/12#PEA-4567#PV/5732#3@
11/09/12#ESE-5577#Xk/8536#2
14/09/12#PNW-1235#HY/7195#2@

这是主要方法的代码:

File orderData = new File("PurchaseOrderData.txt");            
Scanner dataScan = new Scanner(orderData);

while(dataScan.hasNextLine())
{
String lineData = dataScan.nextLine();

Scanner lineScan = new Scanner(lineData);
lineScan.useDelimiter("#");

String date = lineScan.next(); // line 259
String id = lineScan.next();
String code = lineScan.next();

String quantityPlus = lineScan.next();

if(!quantityPlus.contains("@"))
management.addNewPurchaseOrder(date, id, code,
Integer.parseInt(quantityPlus)); // line 267
else
{
quantityPlus = quantityPlus.replace("@", "");
management.addNewPurchaseOrder(date, id, code,
Integer.parseInt(quantityPlus));
management.startNewMonth();
}

关于

的第一个实例
management.addNewPurchaseOrder(date, id, code, Integer.parseInt(quantityPlus));

我得到这个异常:

java.lang.NumberFormatException: For input string: "2 "
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at Assignment2.MainTest.main(MainTest.java:267)

如果我这样做:

String quantityPlus = lineScan.next();
quantityPlus = quantityPlus.replace(" ", "");

我得到以下信息:

java.util.NoSuchElementException
java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at Assignment2.MainTest.main(MainTest.java:259)

MainTest.java:259 - String date = lineScan.next();

我也尝试过使用 nextInt(),但结果是一样的。那里出了什么问题?

非常感谢!

最佳答案

MainTest.java:259 - String date = lineScan.next();您应该先检查该行是否为空,这可能就是扫描器抛出异常的原因

关于java - 输入数据 : java. lang.NumberFormatException:对于输入字符串: "2 ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29321422/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com