I / O Java需要int

时间:2016-06-08 19:27:37

标签: java io try-catch

我正在尝试使用I / O来报告我需要的股票(如果股票低于8)。

它告诉我int需要myShop.listLowStockToFile());;当我添加一个数字时,它告诉我这里不允许void'。我该如何解决这个问题?

  public void listLowStockToFile(int threshhold)
   {
   System.out.println("****The Stock that is getting low is: " + " Minimum " +threshhold + " Report for Bob Shaw****\n");
   for (Item nextItem : items)
   {
   if(nextItem.getNuminStock() < threshhold)
   {
       System.out.println(nextItem);
   }
}
}

public class Report {
    public static void main(String[] args) {
        Shop myShop = new Shop();

        CD cd1 = new CD("Abba Gold", "Abba", 15);
        myShop.addItem(cd1);

        Game game1 = new Game("Chess", 2, 39.95);
        myShop.addItem(game1);

        ElectronicGame eg1 = new ElectronicGame("Shrek", "PS2", 1, 79.50);
        myShop.addItem(eg1);
        ElectronicGame eg2 = new ElectronicGame("Doom", "PC", 2, 30.20);
        myShop.addItem(eg2);
        ElectronicGame eg3 = new ElectronicGame("AFL", "PS2", 2, 49.95);
        myShop.addItem(eg3);


        cd1.receiveStock(3);
        game1.receiveStock(5);
        eg1.receiveStock(10);
        eg2.receiveStock(1);
        cd1.receiveStock(7);


        cd1.sellCopy(true);
        cd1.sellCopy(true);
        eg2.sellCopy(true);

        myShop.listItems();
        myShop.listLowStockToFile(8);
        myShop.listGamesByPlatform("PS2");
        myShop.calcTotalSales();

        Game game2 = new Game("Chess", 2, 39.95);
        myShop.addItem(game2);

        eg2.sellCopy(false);

        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter("LowStock.txt"));
            writer.write("Report dated" + new Date() + "\n");
            writer.write(myShop.listLowStockToFile()); // This line.
            writer.close();
            System.out.println("Report finished");
        } catch (Exception ex) {
            System.out.println("File I/O error" + ex);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您需要listLowStockToFile才能返回字符串:

public String listLowStockToFile(int threshhold) {
   String rtn = "****The Stock that is getting low is: " + " Minimum " +threshhold + " Report for Bob Shaw****\n";
   for (Item nextItem : items) {
      if(nextItem.getNuminStock() < threshhold) {
         rtn += nextItem.toString() + "\n";
      }
   }

   System.out.print(rtn);

   return rtn;
}

原因是BufferedWritter.writeString为参数。