[英]How do I copy values read in from a file into a array list
I am trying to solve a problem which takes a file as an input that has 5 countries data for example: profits items sold yadayada.... I'm trying to read in from the file and tally up the total profits for each country.我正在尝试解决一个问题,该问题将一个文件作为输入,其中包含 5 个国家/地区的数据,例如:销售 yadayada 的利润项目......我试图从文件中读取并计算每个国家/地区的总利润。 Currently I've read inputs from the file found the indexes of the country names and the profits for the countries ( each country and profit are in the same index all throughout the CSV file).
目前,我已经从文件中读取了输入,找到了国家名称的索引和国家的利润(每个国家和利润在整个 CSV 文件中都在同一个索引中)。 I then started to look at each line and comparing whether the line is one of the 5 countries.
然后我开始查看每条线路并比较该线路是否是 5 个国家/地区之一。 If The index of the line says the country name is say, " United States of America" the index that contains the profit should be added to the countries list.
如果该行的索引表示国家名称为“美国”,则应将包含利润的索引添加到国家/地区列表中。 My problem right now is that the array lists that I am trying to create for each country stores the values for all the profits ( like I wanted it to) but then repeats constantly and the final array list is made up of different arrays (I don't know how to put that).
我现在的问题是,我试图为每个国家/地区创建的数组列表存储了所有利润的值(就像我想要的那样)但随后不断重复,最终的数组列表由不同的数组组成(我不不知道怎么说)。
The lists looks like [1234 12341 1241231 1231 3123 124123 123 123 123 ], [3123 123 112358723 8398172 8123 ], [].....列表看起来像 [1234 12341 1241231 1231 3123 124123 123 123 123 ], [3123 123 112358723 8398172 8123 ], [].....
when i want them to look like [1234, 1234,1234,1234,1234,1234]当我希望它们看起来像 [1234, 1234,1234,1234,1234,1234]
I don't know why the array list is copying the values over and over and over into different indexes but that's basically the crux of my problem.我不知道为什么数组列表一遍又一遍地将值复制到不同的索引中,但这基本上是我问题的症结所在。
From there I am supposed to take those values and sum them up for each country and print them to an output file which i feel like i can handle.从那里我应该获取这些值并将它们总结为每个国家,然后将它们打印到我觉得我可以处理的输出文件中。
My CSV file looks something like this,我的 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
And my code thus far looks like :到目前为止我的代码看起来像:
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 + " ");
}
}
Could have probably done this a bit better than the way I did, but I ultimately figured it out可能比我做的更好一点,但我最终想通了
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.