BufferedReader bin = new BufferedReader(new FileReader("C:\\Users\\ASUS\\eclipse-workspace\\MissionMars\\src\\phase-2"));
String line;
ArrayList <Items> point = new ArrayList<Items>();
while((line=bin.readLine()) != null )
{
String [] value = line.split(" = ");
String x=value[0];
int y=Integer.parseInt(value[1]);
Items obj1 = new Items(x,y);
obj1.setName(x);
obj1.setWeight(y);
point.add(obj1);
}
System.out.println(point);我的节目显示:
java.lang.NumberFormatException:用于输入字符串:“5000".
发布于 2020-05-16 20:59:19
作为一种普遍的实践,您应该在处理输入的代码中更加防御性一点。在这种情况下,在异常中所显示的数字字符串中有一个前导空格。你可以用这样的方法解决这个问题:
...
int y = Integer.parseInt(value[1].trim());
...这将处理前导和尾随空格,但是如果数字格式不太好,程序仍然会抛出异常。我建议将解析放在一个try/catch块中,当出现异常时,您可以在其中设置合理或有意义的默认值。
https://stackoverflow.com/questions/61843407
复制相似问题