扫描仪无法读取.txt

时间:2016-10-27 06:55:18

标签: java

我有一个自助结账,在打印收据时将所有购买的商品导出到.txt文件。

public void printReceipt()
{   
    // \t - creates tabs for alignment
    String construct = String.format("%-25s %-8s %-8s %-10s %-10s", "Name", "ID", "Price", "Quantity", "Weight");
    infoField.append(construct + "\n");
    ArrayList<String> exportText = new ArrayList<>();
    for (Item itm : scan) 
    {
        String output = String.format("%-25s %-8s %-8s %-12s %-12s", itm.name, itm.Id, itm.price, itm.getCount(), itm.getWeight());
        infoField.append(output + "\n");
        String itmType = "";
        if (itm instanceof BarcodeItem)
            {
                itmType = "barcode";
                exportText.add(itmType+"-"+itm.name+"-"+itm.Id+"-"+itm.price+"-"+itm.getBarcode()+"-");
            }
        else if (itm instanceof CountedItem)
            {
                itmType = "counted";
                exportText.add(itmType+"-"+itm.name+"-"+itm.Id+"-"+itm.price+"-"+itm.getCount()+"-");
            }
        else if (itm instanceof WeightedItem)
            {
                itmType = "weighted";
                exportText.add(itmType+"-"+itm.name+"-"+itm.Id+"-"+itm.price+"-"+itm.getWeight()+"-");
            }    
    }
    transId++;
    outputStream.println(exportText+"-"+transId);
    outputStream.close();
}

完成此操作后,我使用扫描程序导入.txt文件,并使用分隔符分隔每个块并将其解析为其原始数据类型。

不幸的是,在我的加载文本方法中,调试器显示了String名称;没有被初始化,我不明白为什么?

public void loadText() throws IOException
{
    while(reader.hasNext())
    {
        String type = reader.next();
        String name = reader.next();
        int id = Integer.parseInt(reader.next());
        double price = Double.parseDouble(reader.next());

        if (type.equals("barcode"))
        {
          String  barcode = reader.next();
          Item itm = new BarcodeItem(id, name, price, barcode);
         // aList.addElement(itm.toString());
          System.out.println("Item has been added");
        }
        else if (type.equals("counted"))
        {
           double count = Double.parseDouble(reader.next());
           Item itm = new CountedItem(id, name, price, count);
          // aList.addElement(itm.toString());
           System.out.println("Item has been added");
        }
        else if (type.equals("weighted"))
        {
            double weight = Double.parseDouble(reader.next());
            Item itm = new WeightedItem(id, name, price, weight);
           // aList.addElement(itm.toString());
            System.out.println("Item has been added");
        }
        else
        {
            System.out.println("No file found");
        }
    }
} 

0 个答案:

没有答案