[英]Write a Java program to summarize the data from an input file and output a report
我需要幫助編寫 Java 程序來匯總輸入文件 sprocketorders.txt 中的數據並輸出類似於以下報告的報告:
Spacely Sprockets
Taking Sprockets into the Future
Sales Summary Report
Sprocket Number Total Quantity Sold
1 90
2 155
3 50
4 300
5 100
此圖表中的數據來自我上面命名的 .txt 文件。 此txt文件中包含的信息如下:
3 50
2 20
2 100
5 15
1 90
5 85
4 300
2 35
3 100
該報告需要出現在輸出窗口中。
我想使用開關結構。 這是我必須用作參考的開關結構片段:
switch (snum)
{
case 1: part1total = part1total + quantity;
break;
case 2: part2total = part2total + quantity;
break;
case 3: part3total = part3total + quantity;
break;
case 4: part4total = part4total + quantity;
break;
case 5: part5total = part5total + quantity;
break;
default: System.out.println("Bad sprocket number");
}
這是我到目前為止確定它是從文件輸入的代碼:
package spacely.sprockets;
public class SpacelySprockets
{
public static void main(String[] args)
{
InputFile orderinfo;
orderinfo = new InputFile("sprocketorders.txt");
}
}
如何使用 switch 結構從 txt 文件中匯總數據並輸出報告? 對我來說,如何讓它從 txt 文件中輸入數據並像下面的示例一樣顯示所有數據,這對我來說沒有意義。 我真的只需要一些可靠的方向。 謝謝。
請嘗試以下代碼:
請不要忘記更改文件路徑
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;
public class FileParsingDemo{
public static void main(String args[]) {
try {
TreeMap<String, String> map = new TreeMap<String, String>();
BufferedReader br = new BufferedReader(new FileReader(
"D:/vijay/temp.txt"));
String line;
while ((line = br.readLine()) != null) {
// process the line.
// System.out.println(line);
line = line.trim();
String number = line.substring(0, line.indexOf(" "));
String qlty = line.substring(line.lastIndexOf(" "));
int found=0;
for (int i = 0; i < map.size(); i++) {
if (map.containsKey(number.trim())) {
String oldQlt=map.get(number.trim());
int totalqlt=Integer.parseInt(oldQlt) + Integer.parseInt(qlty.trim());
map.remove(number.trim());
map.put(number, ""+totalqlt);
found=1;
break;
}
}
if(found==0)
{
map.put(number.trim(), qlty.trim());
}
}
br.close();
for (Map.Entry<String, String> e : map.entrySet()) {
//to get key
System.out.println(e.getKey() +" ---- " + e.getValue());
//and to get value
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
更好的方法是:
1、使用HashhMap並保持Rocket Number作為key,Quantity作為value。
2.每次從輸入文件中讀取一個火箭編號時,檢查Map中是否已經存在該編號,如果是,則獲取與該編號相關的數量,將現有數量與從文件中讀取的數量相加並放回地圖。 如果該數字不存在,則將其與數量一起添加到地圖中。
3.按升序對地圖進行排序。
4.將它寫回一個新文件,並使用某種看起來合適的格式。
PS:使用 FileReader 和 BufferedReader 讀取文件。 FileWriter 和 BufferdWriter 寫入文件。
使用緩沖讀取器包裝文件讀取器:
BufferedReader sprocketFileReader = new BufferedReader(
new FileReader("myFile.txt")
);
( http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html )
使用BufferedReader的readLine方法逐行讀取文件
while ( ( line = sprockFileReader.readLine() ) !=null) {...}
使用 String.split 將數字與值分開
您可能還想查看try with resouces以了解正確的流處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.