微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

NumberFormatExeption 使用 BufferedReader 处理双精度

如何解决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 举报,一经查实,本站将立刻删除。