[写入文件时为未知来源

问题描述 投票:0回答:1

我必须在每次更改文件时将其写入文件,即有人更改了数量或某种形式。我选择的方法是使用项目填充ArrayList,将其创建为类,然后将所做的更改将它们字符串返回到文本文件中。

public void readStock() {
    Scanner scanner = null;
    try {
        File input = new File("Stock.txt");
        scanner = new Scanner(input);
        Mouse mouse = null;
        Keyboard keyboard = null;
        while (scanner.hasNextLine()) {
            String[] info = scanner.nextLine().split(",");
            //System.out.println(Connectivity.valueOf(info[5].trim().toUpperCase()) instanceof Connectivity);
             if (info[1].trim().equals("mouse")) {
                mouse = new Mouse(Integer.parseInt(info[0].trim()), 
                                  info[3].trim(), info[4].trim(), 
                                  Connectivity.valueOf(info[5].trim().toUpperCase()),
                                  Double.parseDouble(info[6].trim()),
                                  Double.parseDouble(info[7].trim()), 
                                  Integer.parseInt(info[6].trim()), 
                                  Integer.parseInt(info[9].trim()), 
                                  MouseType.valueOf(info[2].trim().toUpperCase()));
                    productList.add(mouse);
             } else {
                 keyboard = new Keyboard(Integer.parseInt(info[0].trim()), 
                                         info[3].trim(), info[4].trim(), 
                                         Connectivity.valueOf(info[5].trim().toUpperCase()),
                                         Double.parseDouble(info[6].trim()),
                                         Double.parseDouble(info[7].trim()), 
                                         Integer.parseInt(info[6].trim()), 
                                         KeyboardType.valueOf(info[2].trim().toUpperCase()),
                                         KeyboardLayout.valueOf(info[9].trim().toUpperCase()));
                    productList.add(keyboard);
            }

它可以使用默认数据正常工作,但是当我决定添加一个额外的项目时,它会使所有内容都受挫。

public void addStock() {
    BufferedWriter bw = null;
    try {
        bw = new BufferedWriter(new FileWriter("Stock.txt"));
        int max = productList.size();
        for(int i = 0; i < max-2; i++) {
            bw.write(productList.get(i).toString() + "\n");
        }
        bw.write(productList.get(max-1).toString());

    } catch(IOException e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    }
    finally {
        try {
            if(bw != null) {
                bw.close();
            }
        } catch(IOException e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
    }
}

以及它给我的错误:

java.lang.NumberFormatException: For input string: "15.0"
For input string: "15.0"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Reader.readStock(Reader.java:42)
at MainTest.main(MainTest.java:48)

以及文本文件的内容:

112233, mouse, GAMING, Logitech, black, WIRELESS, 15.0, 7.5, 15, 3
123456, keyboard, Corsair, black, WIRED, 2.0, 30.0, 2, GAMING, UK
124455, keyboard, Advent, white, WIRED, 10.0, 3.5, 10, STANDARD, UK
124566, mouse, STANDARD, Advent, grey, WIRED, 15.0, 2.5, 15, 2
125567, keyboard, Logitech, black, WIRELESS, 4.0, 25.99, 4, INTERNET, US
221101, mouse, STANDARD, Logitech, black, WIRED, 3.0, 3.0, 3, 3
221122, mouse, GAMING, Razer, black, WIRED, 1.0, 28.0, 1, 7
223044, mouse, GAMING, Anker, blue, WIRED, 3.0, 16.0, 3, 10
234555, keyboard, Apple, white, WIRELESS, 10.0, 75.5, 10, STANDARD, US
235066, keyboard, Microsoft, black, WIRELESS, 5.0, 45.5, 5, FLEXIBLE, UK
236677, mouse, STANDARD, Asus, blue, WIRELESS, 8.0, 10.0, 7, 5
237788, keyboard, Logitech, grey, WIRED, 15.0, 6.5, 15, STANDARD, UK
java file exception text-files numberformatexception
1个回答
1
投票

您正在将浮点数解析为整数。

将parseInt替换为parseFloat,然后强制转换为int

System.out.println((int)Float.parseFloat(“ 15.0” .trim()));

如果您需要一个值作为浮点数,则不需要强制转换:)

© www.soinside.com 2019 - 2024. All rights reserved.