![](/img/trans.png)
[英]How do I print lines from a command-line input file to a command-line output file?
[英]How to print user input lines from an input file to an output file
我正在嘗試從以下位置執行#4:
程序輸入:
您的程序將向用戶顯示歡迎消息和選項菜單供用戶選擇。
歡迎來到電廠分析儀計划。 請從以下選項中選擇:
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的文件。 您可以使用string 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;
}
通過迭代列表來檢查數據中是否有月份。
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.