gpt4 book ai didi

java - 为什么无法使用 java 中的 Integer.valueOf() 方法解析从文本中读取的实际数字字符串?

转载 作者:搜寻专家 更新时间:2023-10-31 08:21:56 25 4
gpt4 key购买 nike

为什么无法使用 java 中的方法 Integer.valueOf() 解析从文本中读取的实际数字字符串?

异常:

Exception in thread "main" java.lang.NumberFormatException: For input string: "11127"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.valueOf(Integer.java:766)
at sharingBike.ReadTxt.readRecord(ReadTxt.java:91)
at sharingBike.ReadTxt.main(ReadTxt.java:17)

这是我的代码

        File fileView = new File(filePath);

BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileView), "UTF-8"));

String line;

int count = 0;
while ((line = in.readLine()) != null) {
String[] lins = line.split(";");

int value;
value = Integer.valueOf(lins[0]);}

最佳答案

这是你的字符串的内容:

System.out.println(Arrays.toString("11127".getBytes()));

哪些输出:

[-17, -69, -65, 49, 49, 49, 50, 55]

前三个字节是a UTF-8 BOM .

您可以通过先从字符串中删除非数字来修复它(并使用 parseInt 返回 int 而不是 Integer) :

int value = Integer.parseInt(lins[0].replaceAll("\\D", "");

关于java - 为什么无法使用 java 中的 Integer.valueOf() 方法解析从文本中读取的实际数字字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47180647/

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