title: Surround CSV lines with double quotes/ create custom main method
body: <p>I have to take a CSVReader IBM created and make changes that fit my requirements. I am new to java and not sure how to do any except for #1, my requirements are: </p>
  
  <ol>
  <li>take the CSV through the command line </li>
  <li>add a main method</li>
  <li>surround each line with double quotes</li>
  <li>output a new CSV</li>
  </ol>
  
  <p>Ex. Input
  Jordan, Michael, J, ", 23<br>
  Ex. Output
  "Jordan, Michael, J, "", 23"</p>
  
  <p>Background information: this program will be added into a shell script, the functionality is being added because the csv is corrupted if a single double quote is entered</p>
  
  <p>Here is the code:</p>
  
  <pre><code>package Scripts;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.Reader;
  import java.util.ArrayList;
  import java.util.List;
  
  public class CSVReader 
  {
      private BufferedReader br;
      private boolean hasNext = true;
      private static char separator = ' ';
      private static char quotechar = ' '; 
  
      public static final char DEFAULT_SEPARATOR = ',';
      public static final char DEFAULT_QUOTE_CHARACTER = '"';
  
      public static void main(final String[] args) throws IOException {
      final String csvFile = args[0];
      final ArrayList&lt;String[]&gt; allElements = new ArrayList&lt;String[]&gt;();
      final CSVReader csvReader = new CSVReader(br, separator, quotechar, csvFile);
      csvTransformer.readAll();
      csvTransformer.close();
  }
  
      public CSVReader(final Reader reader) throws FileNotFoundException {
          this(reader, DEFAULT_SEPARATOR);
      }
  
      public CSVReader(final Reader reader, final char separator) throws FileNotFoundException {
          this(reader, separator, DEFAULT_QUOTE_CHARACTER, null);
      }
      public CSVReader(final Reader reader, final char separator, final char quotechar, final String csvFile) throws FileNotFoundException {
          CSVTransformer.br = new BufferedReader(new FileReader(csvFile));
          this.separator = separator;
          this.quotechar = quotechar;
      }
  
  
  /**
   * Reads the entire file into a List with each element being a String[] of tokens.
   * 
   * return a List of String[], with each String[] representing a line of the file.
   */
      public List readAll() throws IOException 
      {
          List allElements = new ArrayList();
          while (hasNext) 
          {
              String[] nextLineAsTokens = readNext();
              if (nextLineAsTokens != null) 
                  allElements.add(nextLineAsTokens);
          }
          return allElements;
      }
  
  /**
   * Reads the next line from the buffer and converts to a string array.
   * 
   * return a string array with each comma-separated element as a separate entry.
   */
      public String[] readNext() throws IOException {
          String nextLine = getNextLine();
          return hasNext ? parseLine(nextLine) : null;
      }
  
  /**
   * Reads the next line from the file.
   * 
   * return the next line from the file without trailing newline
   */
      private String getNextLine() throws IOException {
          String nextLine = br.readLine();
          if (nextLine == null) {
              hasNext = false;
          }
          return hasNext ? nextLine : null;
      }
  
  /**
   * Parses an incoming String and returns an array of elements.
   * 
   * @param nextLine
   *            the string to parse
   * @return the comma-tokenized list of elements, or null if nextLine is null
   * @throws IOException if bad things happen during the read
   */
      private String[] parseLine(String nextLine) throws IOException {
          if (nextLine == null) {
              return null;
          }
          List tokensOnThisLine = new ArrayList();
          StringBuffer sb = new StringBuffer();
          boolean inQuotes = false;
          do {
              if (inQuotes) {
              // continuing a quoted section, reappend newline
                  sb.append("\n");
                  nextLine = getNextLine();
                  if (nextLine == null)
                  break;
              }
              for (int i = 0; i &lt; nextLine.length(); i++) {
                  char c = nextLine.charAt(i);
                  if (c == quotechar) {
                  // this gets complex... the quote may end a quoted block, or escape another quote.
                  // do a 1-char lookahead:
                  if(inQuotes)  // we are in quotes, therefore there can be escaped quotes in here.
                          &amp;&amp; nextLine.length() &gt; (i+1)  // there is indeed another character to check.
                          &amp;&amp; nextLine.charAt(i+1) == quotechar )
                      { // ..and that char. is a quote also.
                      // we have two quote chars in a row == one quote char, so consume them both and
                      // put one on the token. we do *not* exit the quoted text.
                          sb.append(nextLine.charAt(i+1));
                          i++;
                      }
                      else
                      {
                          inQuotes = !inQuotes;
                      }
                  }
                  else if (c == separator &amp;&amp; !inQuotes) {
                      tokensOnThisLine.add(sb.toString());
                      sb = new StringBuffer(); // start work on next token
                  }
                  else {
                      sb.append(c);
                  }
              }
          } while (inQuotes);
          tokensOnThisLine.add(sb.toString());
          return (String[]) tokensOnThisLine.toArray(new String[0]);
      }
  
      public void close() throws IOException {
          br.close();
      }
  }
  on
  </code></pre>