[英]How do I copy values read in from a file into a array list
我正在嘗試解決一個問題,該問題將一個文件作為輸入,其中包含 5 個國家/地區的數據,例如:銷售 yadayada 的利潤項目......我試圖從文件中讀取並計算每個國家/地區的總利潤。 目前,我已經從文件中讀取了輸入,找到了國家名稱的索引和國家的利潤(每個國家和利潤在整個 CSV 文件中都在同一個索引中)。 然后我開始查看每條線路並比較該線路是否是 5 個國家/地區之一。 如果該行的索引表示國家名稱為“美國”,則應將包含利潤的索引添加到國家/地區列表中。 我現在的問題是,我試圖為每個國家/地區創建的數組列表存儲了所有利潤的值(就像我想要的那樣)但隨后不斷重復,最終的數組列表由不同的數組組成(我不不知道怎么說)。
列表看起來像 [1234 12341 1241231 1231 3123 124123 123 123 123 ], [3123 123 112358723 8398172 8123 ], [].....
當我希望它們看起來像 [1234, 1234,1234,1234,1234,1234]
我不知道為什么數組列表一遍又一遍地將值復制到不同的索引中,但這基本上是我問題的症結所在。
從那里我應該獲取這些值並將它們總結為每個國家,然后將它們打印到我覺得我可以處理的輸出文件中。
我的 CSV 文件看起來像這樣,
Segment,Country, Product , Discount Band ,Units Sold, Manufacturing Price , Sale Price , Gross Sales , Discounts , Sales , COGS , Profit ,Date,Month Number, Month Name ,Year Government,Canada, Carretera , None ,1618.5,3,20,32370,0,32370,16185,16185,1/1/2014,1, January ,2014 Government,Germany, Carretera , None ,1321,3,20,26420,0,26420,13210,13210,1/1/2014,1, January ,2014 Midmarket,France, Carretera , None ,2178,3,15,32670,0,32670,21780,10890,1/6/2014,6, June ,2014 Midmarket,Germany, Carretera , None ,888,3,15,13320,0,13320,8880,4440,1/6/2014,6, June ,2014 Midmarket,Mexico, Carretera , None ,2470,3,15,37050,0,37050,24700,12350,1/6/2014,6, June ,2014 Government,Germany, Carretera , None ,1513,3,350,529550,0,529550,393380,136170,1/12/2014,12, December ,2014 Midmarket,Germany, Montana , None ,921,5,15,13815,0,13815,9210,4605,1/3/2014,3, March ,2014
到目前為止我的代碼看起來像:
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(new File("sample-csv-file-for-testing-fixed.csv"));
PrintWriter pw = new PrintWriter(new File("Output.csv"));
// gets first line of file
String firstline = in.nextLine();
firstline.trim();
String data = firstline.replaceAll(" ", "");
String[] header = data.split(",");
// find index of Country and Profit and store them into variables
String country = "Country";
String profit = "Profit";
int index1 = 0, index2 = 0;
for (int i = 0; i < header.length; i++) {
if (header[i].equals(country)) {
index1 = i;
}
}
for (int i = 0; i < header.length; i++) {
if (header[i].equals(profit)) {
index2 = i;
}
}
//System.out.println(index1+" "+index2);
// arrays for each country
ArrayList<String> USA = new ArrayList<String>();
ArrayList<String> France = new ArrayList<String>();
ArrayList<String> Germany = new ArrayList<String>();
ArrayList<String> Mexico = new ArrayList<String>();
while (in.hasNextLine()) {
String line = in.nextLine();
String nextline = line.replaceAll(" ", "");
String[] values = nextline.split(",");
// find what country the line has
if (values[index1].equals("United States of America")) {
USA.add(values[index2]);
} else if (values[index1].equals("France")) {
France.add(values[index2]);
} else if (values[index1].equals("Germany")) {
Germany.add(values[index2]);
} else if (values[index1].equals("Mexico")) {
Mexico.add(values[index2]);
}
// test prints
// System.out.print(USA );
//System.out.print("\n" + France + " ");
//System.out.print("\n" + Germany + " ");
//System.out.println("\n"+ Mexico + " ");
}
}
可能比我做的更好一點,但我最終想通了
public static void main(String[]args)throws IOException{
Scanner in = new Scanner(new File("sample-csv-file-for-testing-fixed.csv"));
PrintWriter pw = new PrintWriter(new File("Output.csv"));
// gets first line of file
String firstline = in.nextLine();
firstline.trim();
String data = firstline.replaceAll(" ","");
String[] header = data.split(",") ;
// find index of Country and Profit and store them into variables
String country = "Country";
String profit = "Profit";
int index1 =0 , index2=0;
for(int i = 0;i < header.length;i++){
if(header[i].equals(country)){
index1 = i;
}
}
for(int i = 0;i<header.length;i++){
if(header[i].equals(profit)){
index2 = i;
}
}
// Create arraylists for each country
ArrayList<String> USA = new ArrayList<String>();
ArrayList<String> France = new ArrayList<String>();
ArrayList<String> Germany = new ArrayList<String>();
ArrayList<String> Mexico = new ArrayList<String>();
ArrayList<String> Canada = new ArrayList<String>();
while( in.hasNextLine()){
String line = in.nextLine();
String nextline = line.replaceAll(" ","");
String[] values = nextline.split(",");
//Finding lines with each country
if(values[index1].contains("United")){
USA.add(values[index2]);
}
else if(values[index1].equals("France")){
France.add(values[index2]);
}
else if(values[index1].equals("Germany")){
Germany.add(values[index2]);
}
else if(values[index1].equals("Mexico")){
Mexico.add(values[index2]);
}
else if(values[index1].equals("Canada")){
Canada.add(values[index2]);
}
}
// call method and store values into double array lists;
ArrayList<Double> france_results = getIntegerArray(France);
ArrayList<Double> germany_results = getIntegerArray(Germany);
ArrayList<Double> USA_results = getIntegerArray(USA);
ArrayList<Double> mexico_results = getIntegerArray(Mexico);
ArrayList<Double> canada_results = getIntegerArray(Canada);
// call method to sum values in ArrayLists
double sumUSA = getSum(USA_results);
double sumFrance = getSum(france_results);
double sumGermany = getSum(germany_results);
double sumMexico = getSum(mexico_results);
double sumCanada = getSum(canada_results);
// Print to Output.CSV file
pw.print("France , " + sumFrance + "\n"
+ "Germany ," + sumGermany + "\n"
+ "Canada ," + sumCanada + "\n"
+ "United States of America ," + sumUSA + "\n"
+ "Mexico , "+ sumMexico);
pw.close();
}
// Method to conver String ArrayList to Integer ArrayLists
public static ArrayList<Double> getIntegerArray(ArrayList<String> stringArray){
ArrayList<Double> values = new ArrayList<Double>();
for(String stringValue : stringArray){
values.add(Double.parseDouble(stringValue));
}
return values;
}
// method to sum the index of each ArrayList
public static double getSum(ArrayList<Double> doubleArray){
double sum = 0;
for(int i = 0; i< doubleArray.size();i++){
sum = sum + doubleArray.get(i);
}
return sum;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.