【问题标题】:How to print user input lines from an input file to an output file如何将用户输入行从输入文件打印到输出文件
【发布时间】:2019-05-12 16:52:04
【问题描述】:

我正在尝试从以下位置执行 #4:

程序输入:

您的程序将向用户显示欢迎消息以及供用户选择的选项菜单。

欢迎使用 Power Plant Analyzer 计划。请从以下选项中选择:

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

设计

您需要首先实现一个名为 Entry 的类,该类存储以下信息:

•   Month name
•   Day
•   Year
•   Power output

添加您认为合适的适当方法。

程序选项

选项 1:上传数据

如果用户选择此选项,程序将
一种。提示用户输入包含数据的文件。 湾。将记录读入数组或ArrayList

输入文件格式:

月日年产量

示例输入文件:

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

编写一个名为 UploadData 的方法来完成此任务。选择适当的参数和返回类型。

文件读取完成后,将再次显示主菜单。

选项 2:查看数据

如果用户选择此选项,程序将把读入的数据打印到屏幕上。

样本输出:

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

编写一个名为 PrintData 的方法来完成此任务。选择适当的参数和返回类型。

打印完成后,将再次显示主菜单。

选项 3:下载统计数据

如果用户选择此选项,程序将创建一个包含以下数据的统计文件:

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

统计文件将与输入文件同名,但附加了 _stats.txt。例如,如果输入文件名为 data.txt,则统计文件将命名为 data_stats.txt。

注意:它会在添加 _stats.txt 之前从 data.txt 中删除 .txt。它不会创建名为 data.txt_stats.txt 的文件。您可以使用字符串 substr 方法从文件名中删除最后 4 个字符。

编写一个名为 CreateStatsFile 的方法来完成此任务。选择适当的参数和返回类型。

创建统计文件后,将再次显示主菜单。

选项 4:打印月份

如果用户选择此选项,程序将询问月份名称并进行搜索。它将显示该月的所有数据。如果没有可用的数据,则应显示适当的方法。

编写一个名为 PrintMonth 的方法来完成此任务。选择适当的参数和返回类型。

搜索完成后会再次显示主菜单

我认为我的骨架代码设置正确,但我现在怎么会卡住。

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;
}

【问题讨论】:

    标签: java arrays class methods


    【解决方案1】:

    通过迭代列表检查数据中的月份是否可用。

    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");
            }
        }
    } 
    

    【讨论】:

    • 我在这一行的 uploadResults 下找不到符号:for (Entry uploadResult : uploadResults) {
    • MonthList 不是标准的 java 变量名。我只是为函数参数使用了不同的名称。 public static String printMonth(ArrayList&lt;Entry&gt; uploadResult)
    • 哦,好吧,我明白了,那我的退货声明应该是什么?
    • 我认为您不应该有退货声明。 void 会是更好的选择。
    猜你喜欢
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    • 2014-10-18
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多