title: How do I parse a column from a CSV string using jackson CsvMapper or another csv parser?
body: <p>I have a Java method that receives a CSV string of values and an integer index to reference which column in the CSV string to parse.  The method returns the value associated with the integer index in the CSV string.</p>
  
  <p>For example if I have a CSV string with a header and a second row with values defined as:</p>
  
  <pre><code>String csvString = "Entry #,Date Created,Date Updated, IP Address
                      165,8/22/13 14:46,,11.222.33.444";
  </code></pre>
  
  <p>and the integer index received by the method was 1, I'd expect the method to return the string "165"</p>
  
  <p>And if the integer index received by the method was 2, I'd expect the method to return the string "8/22/13 14:46"</p>
  
  <p>etc,...</p>
  
  <p>I don't want to just split up the CSV string by commas as that could get ugly and I'm sure that there is a CSV parser that already does some parsing like this.  From my Google searches it sounds like OpenCSV or the Jackson CsvMapper can do this.</p>
  
  <p>I've been playing with the com.fasterxml.jackson.dataformat.csv.CsvMapper libary to parse out the appropriate column of this CSV string and here's what I have so far:</p>
  
  <pre><code>int csvFieldIndex (this is the integer index passed into my method)
  
  String csvString = "Entry #,Date Created,Date Updated, IP Address
                      165,8/22/13 14:46,,11.222.33.444";
  
  CsvSchema csvSchema = CsvSchema.emptySchema().withHeader();
  ObjectReader mapper = new CsvMapper().reader(Map.class).with(csvSchema);
  
  MappingIterator&lt;Map&lt;String, Object&gt;&gt; iter = null;
  iter = mapper.readValues(csvString);
  
  while (iter.hasNext()) {
      Map&lt;String, Object&gt; row = iter.next();
  
      System.out.Println("row= " + row.toString());
  }
  </code></pre>
  
  <p>But this iterator gives me all the csv values which is not what what I want; I just want the one value associated with my integer index.</p>
  
  <p>Here's the output I get when I run this snippet of code:</p>
  
  <pre><code>row= {Entry #=165, Date Created=8/22/13 14:46, Date Updated=, IP Address=11.222.33.444}
  </code></pre>
  
  <p>Is there a way I can use the Jackson CsvMapper to do this? </p>
  
  <p>======== UPDATE: ========</p>
  
  <p>Based on feedback from keshlam I was able to parse a column from a CSV with the following code:</p>
  
  <pre><code>CsvSchema csvSchema = CsvSchema.emptySchema().withHeader();
  ObjectReader mapper = new CsvMapper().reader(Map.class).with(csvSchema);
  MappingIterator&lt;Map&lt;String, Object&gt;&gt; iter = null;
  iter = mapper.readValues(csvString);
  
  // iterate over whole csv and store in a map
  Map&lt;String, Object&gt; row = null;
  while (iter.hasNext()) {
      row = iter.next();
  }
  
  // now put the set of field names (keys) from this map into an array
  String[] csvKeysArray = row.keySet().toArray(new String[0]);
  
  int j = 0;
  // loop over this array of keys and compare with csvFieldIndex
  for (int i = 0; i &lt; csvKeysArray.length; i++) {
       // increment j by 1 since array index starts at 0 but input index starts at 1
       j = i + 1;
  
       if (j == csvFieldIndex) {
          csvValue = row.get(csvKeysArray[i]).toString();
       }
  }
  
  return csvValue;
  </code></pre>