gpt4 book ai didi

java - NumberFormatException 在正确的字符串上抛出?

转载 作者:太空宇宙 更新时间:2023-11-04 11:29:24 25 4
gpt4 key购买 nike

我正在尝试将文件解析为 map 。我试图解析的文本(由 sys out 在日志中显示)是

10 号酒店,距 90 米。每个单词之间用 \t

分隔

该文件采用 UTF-8 编码。

这是我的方法:

 private void readFile() {
try {
if (transcriptFile == null)
transcriptFile = new File(transcriptPath);

lines = Files.readAllLines(transcriptFile.toPath());
for (String s : lines) {
if (!s.isEmpty()) {
List<String> parts = Arrays.asList(s.split("\t"));
System.out.println(parts);

int id = Integer.parseInt(parts.get(0).trim());
parts.remove(0);
String text = String.join(" ",parts);
map.put(id,text);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

我遇到了这个异常:

[10, przysuń, hotel, o, 90, metrów ]
java.lang.NumberFormatException: For input string: "10"
at
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at Controller.readFile(Controller.java:143)
at Controller.access$000(Controller.java:29)
at Controller$SpeechTask.call(Controller.java:202)
at Controller$SpeechTask.call(Controller.java:154)
at javafx.concurrent.Task$TaskCallable.call(Task.java:1423)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:748)

我认为没有理由不能解析它。

最佳答案

您的输入文件可能包含 Byte Order Mark (BOM) ,一个不可见的字符,可以位于字符 10 之前。尝试 this post 中的解决方案或 Apache 的BOMInputStream

以非编程方式,您可以使用 Notepad++ 的编码为不带 BOM 的 UTF-8功能并保存输入文件。

关于java - NumberFormatException 在正确的字符串上抛出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43964857/

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