英文:
Exception in thread "main" java.lang.NumberFormatException: For input string: "0.353"
问题
我在Java中将一个字符串解析为浮点数。
但是它抛出了这个异常。我对这个问题感到非常困惑,因为"0.353"显然是一个应该由parseFloat()方法解析的数字。
我有什么遗漏吗?感谢您的帮助!
String FitMappath = PathofFile.path+"FitnessMap.txt";
FileInputStream in = new FileInputStream(FitMappath);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
//逐行读取文件内容
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split("\\s+"); //将行分割为三个元素
float fitness = Float.parseFloat(parts[2]);// 字符串转浮点数
}
异常线程 "main" java.lang.NumberFormatException: 输入字符串无效: "0.353"
at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
at java.base/jdk.internal.math.FloatingDecimal.parseFloat(FloatingDecimal.java:110)
at java.base/java.lang.Float.parseFloat(Float.java:549)
at DelFitnessCalculation.<init>(DelFitnessCalculation.java:66)
at Main.main(Main.java:49)
输入文件的前三行:
Q0085 ATP6 0.353
YDR034C-A YDR034C-A 0.359
tORF13 tORF13 0.360
英文:
I am parsing a string to a float in Java.
But it throws this exception. I am quite confused with thisproblem since "0.353" is obviously a number which should be parsed by the parseFloat() method.
Did I miss something? Appreciate your help!
String FitMappath = PathofFile.path+"FitnessMap.txt";
FileInputStream in = new FileInputStream(FitMappath);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
//Read File Line By Line
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split("\\s+"); //Split into three elements
float fitness = Float.parseFloat(parts[2]);// String to float
}
Exception in thread "main" java.lang.NumberFormatException: For input string: "0.353"
at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
at java.base/jdk.internal.math.FloatingDecimal.parseFloat(FloatingDecimal.java:110)
at java.base/java.lang.Float.parseFloat(Float.java:549)
at DelFitnessCalculation.<init>(DelFitnessCalculation.java:66)
at Main.main(Main.java:49)
First three lines of my input file:
Q0085 ATP6 0.353
YDR034C-A YDR034C-A 0.359
tORF13 tORF13 0.360
答案1
得分: 2
当我执行 System.out.println(Float.parseFloat("0.353"));
时,它会输出 0.353
作为浮点数,因此你的分割可能出了问题,或者可能某处有隐藏字符。
你能否尝试提供完整的代码片段,以便我们能够更好地回答?
英文:
When I do System.out.println(Float.parseFloat("0.353"));
, it prints out 0.353
as a float, so it must either be something wrong with your split or maybe there's a hidden character somewhere.
Could you try giving your entire code snippet in hopes of us being able to answer better?
答案2
得分: 0
把这个代码放在你分享的两行代码之间:
System.out.println("parts[2]=" + parts[2]);
我认为你会找到你正在寻找的东西,也就是导致错误的原因。
英文:
Place this:
System.out.println("parts[2]=" + parts[2]);
between the two lines of code that you shared. I think you will find what you're looking for, which is the cause of your error.
答案3
得分: 0
谢谢你的回答。最终问题原因是UTF-16引起的。
我的输入文件最初是一个Excel文件,然后我将其保存为UTF-16文本文件,这导致了这个问题。我尝试将输入文件另存为制表符分隔的文本文件,结果符合预期。
这是一个奇怪的NumberFormatException(数字格式异常)的原因。希望能帮助其他遇到相同问题的人。
英文:
Thanks for your anwsers. Eventually, it turns out that UTF-16 caused the problem.
My input file was originally an excel file, then I saved it as UTF-16 txt, which caused this problem. I tried to save the input file as tab-delimited text file and it worked as expected.
This is a weird cause of the NumberFormatException. Hope it helps others come across the same problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论