【问题标题】:JAVA - Incorporating y/n Input to Print Array list to a fileJAVA - 将打印数组列表的 y/n 输入合并到文件中
【发布时间】:2019-08-01 23:14:00
【问题描述】:

我目前正在开发一个旨在为汽车经销商存储库存的程序。当用户被提示使用 y/n 选项“您想将库存打印到文件中吗?(y/n)”时,我正在尝试找到将存储在数组列表中的数据打印到文件的正确方法。

到目前为止,我有两种不同的方法。一种显示我输入到数组中的数据,另一种将数据打印到文件中。我只需要弄清楚如何将问题和打印到文件方面添加到显示方法。

  // method to save vehicle data to a file upon exiting the program
  public static void printToFile(String filename) throws FileNotFoundException {

        PrintWriter pw = new PrintWriter(filename);

        String text = " Make | Model| Color| Year| Mileage\n";

        for (Automobile a : carList) {

              text += a.toCSV() + "\n"; // Separating car information with commas
        }

        pw.write(text);

        pw.flush();

        pw.close();

        System.out.println("\n Car Inventory below has been printed to " + filename + " file. \n");

        System.out.println(text);

  }

  // My method to display the data however I just need to find a way to incorporate the y/n question for printing to a file also
  public static void displayCars() { // method to display all vehicles in the list

        Scanner scnr = new Scanner(System.in);

        System.out.println("--------------");
        System.out.println("Car Inventory");
        System.out.println("--------------");

        for (Automobile a : carList) {
            System.out.println(a + "\n");

            // FIND OUT HOW TO ADD y/n INPUT FOR PRINTING TO FILE
        }
  }

希望在 Display 方法中添加一个 y/n 问题,询问用户是否要将数据打印到文件中。

【问题讨论】:

    标签: java file input output printwriter


    【解决方案1】:

    假设您要遍历列表中的每辆汽车并询问用户是否希望将其添加到文件中,如下所示应该可以:

    public static List<Automobile> displayCars() { 
        Scanner scnr = new Scanner(System.in);
        List<Automobile> fileItems = new ArrayList<>();
    
        System.out.println("--------------");
        System.out.println("Car Inventory");
        System.out.println("--------------");
    
        for (Automobile a : carList) {
            System.out.println(a + "\n");
            System.out.println("Would you like to add this item to the file? (y/n)");
            String input = scnr.next();
    
            if (input.equals("y")) {
                fileItems.add(a);
                System.out.println("Added to file.");
            }
        }
    
        return fileItems;
    }
    

    该方法现在只返回应添加到文件中的汽车列表。

    注意:我还没有编译/测试,所以可能会有一些错误。

    【讨论】:

      【解决方案2】:

      如果这是一个命令行程序,那么您可以使用 Scanner 来读取用户的输入。类似以下的方法会起作用:

      // Creates a new Scanner that reads from standard in
      Scanner userInput = new Scanner(System.in);
      String yesNo = userInput.next(); // blocks the program until something is typed into the command line
      if (yesNo.toLowerCase().equals("y")) {
          // print to file
      } else {
          // continue doing something else
      }
      

      .toLowerCase() 不是必需的,但我建议它处理用户输入的大小写更改。

      更多信息请参见Scanner。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-28
        • 2014-05-01
        • 1970-01-01
        • 2018-02-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多