gpt4 book ai didi

java - NumberFormatException 无法从文本文件解析字符串`

转载 作者:行者123 更新时间:2023-12-02 13:18:30 27 4
gpt4 key购买 nike

我在将字符串解析为原语时遇到问题。

File file1 = new File("bankData.txt");

Scanner sc = new Scanner(file1);

while (sc.hasNext())
{
char c = sc.next().charAt(0);

if(c == 's')
{
long l = Long.parseLong(sc.next().trim());
String s1 = sc.next().trim();
String s2 = sc.next().trim();
// Line 248 int in = Integer.parseInt(sc.next().trim());
String s3 = sc.next().trim();
double d1 = Double.parseDouble(sc.next().trim());
double d2 = Double.parseDouble(sc.next().trim());

System.out.println(s1);
System.out.println(l);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(in);
System.out.println(d1);
System.out.println(d2);
}
}

正在生成以下代码

bankData.txtjava.lang.NumberFormatException: For input string:         "6131231234"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at bank.Bank.readFile(Bank.java:248)

这是我的程序尝试读取的bankData.txt 文件。

s 1001 John Doe 6131231234 john@john.com 1000.00 0.01 200.00

起初我认为空格是导致问题的原因,但正如你所看到的,我已经修剪了扫描仪返回的所有字符串,试图消除这个问题,但没有成功。看来原始变量 long l = Long.parseLong(sc.next().trim());
字符串 s1 = sc.next().trim();
字符串 s2 = sc.next().trim();
已成功分配;我尝试在 Eclipse 调试器中确认这一点,但它只是跳过了断点(有任何见解吗?)。 Java SE8 NumberFormatExceptionStates

Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.

不知道该去哪里。

提前致谢,感谢您的努力。

最佳答案

您正在尝试转换大于 Java 中整数最大大小的数字。

最大整数值为 2,147,483,647。您尝试转换为整数的字符串将包含值 6,131,231,234

将其存储为一个长值,如下所示

long x = Long.parseLong(sc.next().trim());

关于java - NumberFormatException 无法从文本文件解析字符串`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43670933/

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