使用BufferedReader处理double时的java NumberFormatException
我在尝试将字符串数组中的字符串转换为双精度字符串时遇到问题
public class ImportFruitAndVegatables {
List<Product> productList = new ArrayList<>();
public List<Product> fillListWithProducts(){
String filePath = "src/Resources/FruitAndVegetablesList.csv";
String splitCsvBy = ",";
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
String line = "";
line = bufferedReader.readLine(); //Reads the first line, without doing anything with it.
System.out.println("try");
while ((line = bufferedReader.readLine()) != null){
String [] splitArray = line.split(splitCsvBy);
System.out.println("while");
System.out.println(splitArray[0]);
System.out.println(Integer.parseInt(splitArray[1]));
System.out.println(Double.parseDouble(splitArray[2]));
Product product = new Product(splitArray[0],Double.parseDouble(splitArray[1]),Integer.parseInt(splitArray[2]));
productList.add(product);
}
} catch (FileNotFoundException e) {
System.out.println("The file was not found ");
e.printStackTrace();
} catch (IOException e) {
System.out.println(); //TODO: print why the IOException occurred.
e.printStackTrace();
}
return productList;
}
}
这是我得到的解释:
Exception in thread "main" java.lang.NumberFormatException: For input string: "2.5"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at ImportData.ImportFruitAndVegatables.fillListWithProducts(ImportFruitAndVegatables.java:28)
at Controllers.MainController.runProgram(MainController.java:12)
at Main.main(Main.java:8)
我的csv看起来像这样:
产品、价格、条形码
苹果,2.51159
橙色,2.51606
芒果,121603
梨,2.51148
问题是,它在Integer上完全可以正常工作。parseInt() 我试过用逗号代替句号,但也没用。我希望你能帮助我!:)
我的产品类构造函数如下所示:
公共产品(字符串名称、双倍价格、整数条形码)
共 (0) 个答案