title: How do I copy values read in from a file into a array list
body: <p>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. 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). I then started to look at each line and comparing whether the line is one of the 5 countries. If The index of the line says the country name is say, &quot; United States of America&quot; 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).</p>
  <p>The lists looks like [1234 12341 1241231 1231 3123 124123 123 123 123 ], [3123 123 112358723 8398172 8123 ], [].....</p>
  <p>when i want them to look like [1234, 1234,1234,1234,1234,1234]</p>
  <p>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.</p>
  <p>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.</p>
  <p>My CSV file looks something like this,</p>
  <pre> 
  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
  </pre>
  <p>And my code thus far looks like :</p>
  <pre><code>public static void main(String[] args) throws IOException {
      Scanner in = new Scanner(new File(&quot;sample-csv-file-for-testing-fixed.csv&quot;));
      PrintWriter pw = new PrintWriter(new File(&quot;Output.csv&quot;));
  
      // gets first line of file
      String firstline = in.nextLine();
      firstline.trim();
      String data = firstline.replaceAll(&quot; &quot;, &quot;&quot;);
      String[] header = data.split(&quot;,&quot;);
  
      // find index of Country and Profit and store them into variables
      String country = &quot;Country&quot;;
      String profit = &quot;Profit&quot;;
  
      int index1 = 0, index2 = 0;
      for (int i = 0; i &lt; header.length; i++) {
          if (header[i].equals(country)) {
              index1 = i;
          }
      }
      for (int i = 0; i &lt; header.length; i++) {
          if (header[i].equals(profit)) {
              index2 = i;
          }
      }
      //System.out.println(index1+&quot; &quot;+index2);
  
      // arrays for each country
      ArrayList&lt;String&gt; USA = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; France = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; Germany = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; Mexico = new ArrayList&lt;String&gt;();
      
      while (in.hasNextLine()) {
          String line = in.nextLine();
          String nextline = line.replaceAll(&quot; &quot;, &quot;&quot;);
          String[] values = nextline.split(&quot;,&quot;);
          // find what country the line has
  
          if (values[index1].equals(&quot;United States of America&quot;)) {
              USA.add(values[index2]);
          } else if (values[index1].equals(&quot;France&quot;)) {
              France.add(values[index2]);
          } else if (values[index1].equals(&quot;Germany&quot;)) {
              Germany.add(values[index2]);
          } else if (values[index1].equals(&quot;Mexico&quot;)) {
              Mexico.add(values[index2]);
          }
  
          // test prints
          // System.out.print(USA );
          //System.out.print(&quot;\n&quot; + France + &quot; &quot;);
          //System.out.print(&quot;\n&quot; + Germany + &quot; &quot;);
          //System.out.println(&quot;\n&quot;+ Mexico + &quot; &quot;);    
      }
  }
  </code></pre>