Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA-合并y/n输入以将数组列表打印到文件中_Java_File_Input_Output_Printwriter - Fatal编程技术网

JAVA-合并y/n输入以将数组列表打印到文件中

JAVA-合并y/n输入以将数组列表打印到文件中,java,file,input,output,printwriter,Java,File,Input,Output,Printwriter,我目前正在制定一个计划,旨在为汽车经销商存储库存。我正在尝试找到将存储在数组列表中的数据打印到文件中的正确方法,此时系统会提示用户使用y/n选项“是否要将清单打印到文件?(y/n)” 到目前为止,我有两种不同的方法。一个显示我输入到数组中的数据,另一个将数据打印到文件中。我只需要弄清楚如何将问题和打印到文件方面添加到显示方法中 // method to save vehicle data to a file upon exiting the program public static v

我目前正在制定一个计划,旨在为汽车经销商存储库存。我正在尝试找到将存储在数组列表中的数据打印到文件中的正确方法,此时系统会提示用户使用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
        }
  }

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

假设您希望迭代列表中的每辆汽车,并询问用户是否希望将其添加到文件中,类似于以下内容的操作应能奏效:

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;
}
// 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
}
公共静态列表displayCars(){
扫描仪scnr=新扫描仪(System.in);
List fileItems=new ArrayList();
System.out.println(“--------------”;
系统输出打印项次(“汽车库存”);
System.out.println(“--------------”;
汽车(a:卡利斯特){
System.out.println(a+“\n”);
System.out.println(“是否要将此项添加到文件中?(y/n)”;
字符串输入=scnr.next();
if(输入等于(“y”)){
添加(a);
System.out.println(“添加到文件”);
}
}
返回文件项;
}
该方法现在只返回应该添加到文件中的汽车的列表


注意:我尚未编译/测试,因此可能存在一些错误。

如果这是一个命令行程序,那么您可以使用扫描仪读取用户的输入。类似于以下内容的操作将起作用:

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;
}
// 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()不是必需的,但我建议它处理来自用户输入的更改

有关更多信息,请参阅