简体   繁体   English

如何将用户输入行从输入文件打印到输出文件

[英]How to print user input lines from an input file to an output file

I'm trying to do #4 from: 我正在尝试从以下位置执行#4:

Program Input: 程序输入:

Your program will display a welcome message to the user and a menu of options for the user to choose from. 您的程序将向用户显示欢迎消息和选项菜单供用户选择。

Welcome to the Power Plant Analyzer program. 欢迎来到电厂分析仪计划。 Please choose from the following options: 请从以下选项中选择:

1.  Upload data 
2.  View data 
3.  Download statistics 
4.  Print Month 
5.  Exit the program 

Design 设计

You will need to start by implementing a class called Entry which stores the following information: 您需要首先实现一个名为Entry的类,该类存储以下信息:

•   Month name
•   Day
•   Year
•   Power output

Add appropriate methods as you see fit. 根据需要添加适当的方法。

Program Options 程序选项

Option 1: Upload Data 选项1:上传数据

If the user chooses this option, the program will 如果用户选择此选项,程序将
a. 一种。 Prompt the user for the file that contains the data. 提示用户输入包含数据的文件。 b. Read in the records into an array or an ArrayList 将记录读入数组或ArrayList

Input File Format: 输入文件格式:

Month Day Year Output 月日年产量

Sample Input File: 样本输入文件:

January 10 2018 236.9 
January 11 2018 267.6 
January 12 2018 278.1 

Write a method called UploadData to do this task. 编写一个名为UploadData的方法来执行此任务。 Choose the appropriate parameters and return type. 选择适当的参数和返回类型。

Once done reading in the file, the main menu will be displayed again. 读完文件后,将再次显示主菜单。

Option 2: View Data 选项2:查看数据

If the user chooses this option, the program will print to the screen the data read in. 如果用户选择此选项,程序将在屏幕上打印读取的数据。

Sample Output: 样本输出:

Date: January 10, 2018 Output: 236.9 
Date: January 11, 2018 Output: 267.6 
Date: January 12, 2018 Output: 278.1 

Write a method called PrintData to do this task. 编写一个称为PrintData的方法来执行此任务。 Choose the appropriate parameters and return type. 选择适当的参数和返回类型。

Once done printing, the main menu will be displayed again. 完成打印后,将再次显示主菜单。

Option 3: Download Statistics 选项3:下载统计信息

If the user chooses this option, the program will create a statistics file with the following data: 如果用户选择此选项,则程序将使用以下数据创建统计文件:

a.  Power output sorted from lowest to highest
b.  Day with highest output 
c.  Total by month 
d.  Average power output for all the data

The statistics file will have the same name as the input file but with _stats.txt appended to it. 统计信息文件的名称与输入文件的名称相同,但附加了_stats.txt。 For example, if the input file was named data.txt, the stats file will be named data_stats.txt. 例如,如果输入文件名为data.txt,则统计文件将名为data_stats.txt。

Note: It will remove the .txt from data.txt, before adding the _stats.txt. 注意:在添加_stats.txt之前,它将从data.txt中删除.txt。 It will NOT create a file with the name data.txt_stats.txt. 它将不会创建名为data.txt_stats.txt的文件。 You can use the string substr method to remove the last 4 characters from the file name. 您可以使用string substr方法从文件名中删除最后4个字符。

Write a method called CreateStatsFile to do this task. 编写一个名为CreateStatsFile的方法来执行此任务。 Choose the appropriate parameters and return type. 选择适当的参数和返回类型。

Once done creating the statistics file, the main menu will be displayed again. 完成创建统计文件后,将再次显示主菜单。

Option 4: Print Month 选项4:列印月份

If the user chooses this option, the program will ask for the month's name and will search for it. 如果用户选择此选项,程序将要求输入月份的名称并进行搜索。 It will display all the data for that month. 它将显示该月的所有数据。 If no data is available, an appropriate method should be displayed. 如果没有可用数据,则应显示适当的方法。

Write a method called PrintMonth to do this task. 编写一个称为PrintMonth的方法来执行此任务。 Choose the appropriate parameters and return type. 选择适当的参数和返回类型。

Once done searching, the main menu will be displayed again 搜索完成后,将再次显示主菜单

I think I have my skeleton code set up properly but how do i'm now stuck. 我想我已经正确设置了我的框架代码,但是现在我怎么卡住了。

public static String printMonth(ArrayList<Entry> MonthList) throws /*what?*/ {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("What is the month's name?");
    String month = keyboard.nextLine();

    for (int i = 0; i < MonthList.size(); i++)
        MonthList.get(i).print();

    Scanner fileIn = new Scanner(new File("data.txt"));
    fileIn.nextLine();
    if (fileIn.hasNext("January")) {

    }

    return month;
}

Check if month is available in the data by iterating the list. 通过迭代列表来检查数据中是否有月份。

public static String printMonth(ArrayList<Entry> uploadResult) throws /*what?*/ {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("What is the month's name?");
    String month = keyboard.nextLine(); 
    boolean monthPresent = false;
    for (Entry uploadResult : uploadResults) {
        if ( uploadResult.getMonth().equals(month)){
            monthPresent = true;
            System.out.println(uploadResult.getDay() + "--" + uploadResult.getPowerOutput());
        }

        if( !monthPresent ){
            System.out.println("Month info not available");
        }
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM