如何从3 List读取到txt文件



我有3个不同的ArrayList对象,项目,商店和库存。我正试图从这些列表中阅读并打印一个格式良好的文本文件,称为库存。我的问题是从多个数组列表中读取。当我从一个列表中阅读和打印而不是从许多列表中打印时,它工作得很好。我想不明白。

下面是我从单个List中打印的代码:

public static void write(List<Item> items, PrintStream out) {
    out.println();
    out.println("Inventory Report");
    out.println("----------------");
    int i = 0;
    for (Item item : items) {
        out.format("%3d. %10s %50s %7.2f %3d%% %7.2f%n", ++i, item.getProductNumber(), item.getDescription(),
                (float) (item.getPrice() / 100.0), (int) (item.getDiscount() * 100),
                (float) (item.getSalePrice() / 100.0));
    }
}

这是我的txt文档的输出:

库存报告
  1. 0210919 Moto精确贴合后雨刷片7.99 0% 7.99
  2. 83-4567-0伊斯顿隐身反射复合曲棍球棒89.99 50% 44.99
  3. 43-0439-6 Tassimo T46家用酿造系统179.99 30% 125.99
  4. 60-3823-0 Yardworks 4吨原木分离器399.99 0% 399.99

现在,为了从3 ArrayList中打印,我使用了相同的模式,有3个循环:

public static void write(List items, List store, List stocks, PrintStream out) {

    out.println();
    out.println("Inventory Report");
    out.println("----------------");
    int i = 0;
    for (Item item : items) {
        for (Store store : stores) {
            for (Stock stock : stocks) {
                out.format("%3d. %10s %50s %7.2f %3d%% %7.2f %5s %20s %4d %7.2%n", ++i, item.getProductNumber(), item.getDescription(),
                        (float) (item.getRetailPrice() / 100.0), (int) (item.getDiscount() * 100),
                        (float) (item.getSalePrice() / 100.0), store.getStoreID(), store.getDescription(),
                        stock.getItemCount(), item.getRetailPrice() * stock.getItemCount());
            }
        }
    }
}

}

但是我的输出没有给我任何东西:

库存报告

有人知道为什么它不打印任何东西吗?我真不明白为什么。谢谢你

编辑:

我的列表来自我读的3个txt文档,也许那里是错误的,但我不明白为什么它会为一个而不是为另一个工作。下面是我的代码:

public static List<?> read(File file) throws ApplicationException {

    Scanner scanner = null;
    try {
        scanner = new Scanner(file);
    } catch (FileNotFoundException e) {
        throw new ApplicationException(e);
    }
    List<Item> items = new ArrayList<Item>();
    List<Store> stores = new ArrayList<Store>();
    List<Stock> stocks = new ArrayList<Stock>();
    try {
        scanner.nextLine();
        while (scanner.hasNext()) {
            String row = scanner.nextLine();
            String[] elements = row.split("\|");
            if (file.getName().equals("items.txt") && elements.length != 4) {
                throw new ApplicationException(String.format("Expected 4 elements but got %d", elements.length));
            } else if (file.getName().equals("stores.txt") && elements.length != 8) {
                throw new ApplicationException(String.format("Expected 8 elements but got %d", elements.length));
            } else if (file.getName().equals("stock.txt") && elements.length != 3) {
                throw new ApplicationException(String.format("Expected 3 elements but got %d", elements.length));
            }
            try {
                if (file.getName().equals("items.txt")) {
                    items.add(new Item(elements[0], elements[1], Integer.valueOf(elements[2]), Float
                            .valueOf(elements[3])));
                } else if (file.getName().equals("stores.txt")) {
                    stores.add(new Store(elements[0], elements[1], elements[2], elements[3], elements[4],
                            elements[5], elements[6], elements[7]));
                } else if (file.getName().equals("stock.txt")) {
                    stocks.add(new Stock(elements[0], elements[1], Integer.valueOf(elements[2])));
                }
            } catch (NumberFormatException e) {
                throw new ApplicationException(e);
            }
        }
        LOG.info("Input file successfully parsed.");
    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }
    LOG.info("List of Input created");
    return items;
}

和我阅读的文本文件:Items.txt

描述| SKU | |零售价格的折扣-Tassimo T46家用酿造系统|43-0439-6|17999|0.30-Moto精确贴合后雨刷片|0210919|799|0.0-伊斯顿隐形反射复合曲棍球棒|83-4567-0|8999|0.5-Yardworks 4吨原木分离器|60-3823-0|39999|0

Stores.txt

-ID|描述|街道|城市|省份|邮政编码|商店电话|汽车服务-BC001 | GRANDVIEW,BENTALL| BENTALL Street 2830 |Vancouver|BC|V5M 4H4|604-431-3570|604-431-3572-BC002|BURNABY SOUTH| 7200 Market Crossing| BURNABY |BC|V5J 0A2|604-451-5888|604-451-5888

Stock.txt

-商店ID|商品SKU|商品数量-BC001 | 43-0439-6 | 25-BC001 | 63 | 60-3823-0-BC001 | 83-4567-0 | 15-BC001 | 0210919 - 0 | 2-BC002 | 43-0439-6 | 12-BC002 | 60-3823-0 | 47岁-BC002 | 83-4567-0 | 32-BC002 | 0210919 - 0 | 0

您的错误是您总是返回items列表。所以其他列表是空的。并且你的代码在打印中将不工作,因为只有items列表不空,其他列表空。您可以在调试时检查。

如果你想修复你的代码,你需要在返回时添加If语句,并根据文件名检查你需要返回的列表。

看来你只返回List<Item> items,我认为你应该返回一个List<List<?>>或一个类,将包装所有的列表,然后修改你的写作原型,像这样:

public static void write(List<List<?>> lists, PrintStream out);

顺便说一下,你不应该使用像List itemsList<Items> items这样的非类型化参数。

List<List<?>> lists = new ArrayList<List<?>>();
List<Item> items = new ArrayList<Item>();
List<Store> stores = new ArrayList<Store>();
List<Stock> stocks = new ArrayList<Stock>();
try
{
    ...
}
lists.add(items);
lists.add(stores);
lists.add(stocks);
return lists;

使用JDK 7编译:

public static List<List<?>> listOfList()
{
    List<List<?>> lists = new ArrayList<>();
    List<Integer> items = new ArrayList<>();
    List<Double> stores = new ArrayList<>();
    List<Float> stocks = new ArrayList<>();
    lists.add(items);
    lists.add(stores);
    lists.add(stocks);
    return (lists);
}

for循环变成:

for (Item item : lists.get(0)) {
    for (Store store : lists.get(1)) {
        for (Stock stock : lists.get(2)) {
            ...
        }
    }
}

也许你在格式化时犯了一个错误?

%3d.    ++i
%10s    item.getProductNumber()
%50s    item.getDescription()
%7.2f   (float) (item.getRetailPrice() / 100.0)
%3d%%   (int) (item.getDiscount() * 100)
%7.2f   (float) (item.getSalePrice() / 100.0)
%5s     store.getStoreID()
%20s    store.getDescription()
%4d     stock.getItemCount()
%7.     item.getRetailPrice()
2%n"    stock.getItemCount()

//对不起,我刚刚发现我的错误

最新更新