Float.intBitsToFloat抛出java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
我试图把这个字符串10909116变成一个浮点数。当使用Float.intBitsToFloat时,我得到一个NumberFormatException。我试图通过将其设置为整数来解决此问题,但出现了以下错误:Float.intBitsToFloat抛出java.lang.NumberFormatException.forInputString(NumberFormatException.java:68),java,Java,我试图把这个字符串10909116变成一个浮点数。当使用Float.intBitsToFloat时,我得到一个NumberFormatException。我试图通过将其设置为整数来解决此问题,但出现了以下错误: at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68) at java.base/java.lang.Integer.parseInt(Integer.java:
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Integer.parseInt(Integer.java:658)
at java.base/java.lang.Integer.valueOf(Integer.java:963)
at Main.main(Main.java:32)
这是以下代码:
String file = Files.readString(Path.of(dir));
String[] pos = file.split("[ \n]");
for(int i = 0; i < pos.length; i++) {
temp = "" + Float.intBitsToFloat(Integer.valueOf(pos[i], 10));
pos[i] = temp;
}
String file=Files.readString(Path.of(dir));
字符串[]pos=file.split([\n]”);
对于(int i=0;i
代码没有问题。
问题是该文件包含方法无法转换为int的垃圾。
您可以删除所有不需要的数字
pos[i]=pos[i].replaceAll("[^0-9]", "");
堆栈跟踪则相反
Integer.valueOf
导致错误,而不是Float.intBitsToFloat
pos[I]
似乎不包含正确格式的整数字符串。