NumberFormatExeption 使用 BufferedReader 处理双精度

问题描述

我在尝试将 String 从我的 String 数组转换为 double 时遇到问题。

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.5,1159

橙色,2.5,1606

芒果,12,1603

梨,2.5,1148

问题是它与 Integer.parseInt() 一起工作得很好 我尝试使用逗号而不是句点,这也不起作用。我希望你能帮助我! :)

我的产品类构造函数如下所示:

公共产品(字符串名称,双倍价格,整数条码)

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)