title: How do I get the function to print out a 2D array into two files in java?
body: <p>My goal is to take a file with names and numbers, the first row will name the columns, the rows following will be the data. Column 0 will have the first name and 1 will have the last name which will be combined. Columns 2, 3, and 4 will contain 3 numbers that will be averaged together, columns 5 to 11 will also contain numbers that will be averaged together. This will be the test data:</p>
  
  <pre><code>first, last, #1, #2, #3, #4, #5, #6, #7, #8, #9, #10
  Dan, Smith, 56, 58, 55, 66, 44, 78, 60.33, 52.33, 70.66, 44
  Sam, Will, 77, 88, 55, 99, 77, 10, 62, 65, 59, 62
  </code></pre>
  
  <p>From here the two output files will contain the same information, the second will have the information spaced out in a neater fashion. What I want the out puts to be are:</p>
  
  <pre><code>name, exam score,hw score,min score
  Dan Smith, 56.33, 62.66, 56.33
  Sam Will, 73.33, 62, 62
  </code></pre>
  
  <p>This line will just be used for the second output file:</p>
  
  <pre><code>out.printf("%20s: %10.2f, %8.2f, %9.2f\r\n",name, average1, average2, smallestaverage);
  </code></pre>
  
  <p>My code is as follows, I tried to test and see if I could print out parts of the data using a for loop but would only receive the original values set for the two variables:</p>
  
  <pre><code>import java.util.Scanner;
  import java.util.ArrayList;
  import java.io.File;
  import java.io.PrintWriter;
  public class compute_grades {
      public static void process_grades(String input_name, String csv_name, String pretty_name) {
          String[][] data = read_spreadsheet(input_name);
          String csvname = "csv_name.txt";
          String pretty = "pretty_name.txt";
          PrintWriter out = null;
          try {
              out = new PrintWriter(input_name);
          } catch (Exception e) {
              System.out.printf("Error: failed to open file %s.\n", input_name);
              System.exit(0);
          }
          String first = "dog";
          String last = "dat";
          for (int i = 0; i &lt; data.length; i++) {
              first = data[i][0];
              last = data[i][1];
          }
          System.out.println(first + last);
      }
      public static String[][] read_spreadsheet(String filename) {
          ArrayList &lt; String &gt; lines = read_file(filename);
          if (lines == null) {
              return null;
          }
          int rows = lines.size();
          String[][] result = new String[rows][];
          for (int i = 0; i &lt; rows; i++) {
              String line = lines.get(i);
              String[] values = line.split(",");
              result[i] = values;
          }
          return result;
      }
      public static ArrayList &lt; String &gt; read_file(String filename) {
          File temp = new File(filename);
          Scanner input_file;
          ArrayList &lt; String &gt; result = new ArrayList &lt; String &gt; ();
          try {
              input_file = new Scanner(temp);
          } catch (Exception e) {
              System.out.printf("Failed to open file %s\n", filename);
              return result;
          }
          while (input_file.hasNextLine()) {
              String line = input_file.nextLine();
              result.add(line);
          }
          input_file.close();
          return result;
      }
      public static void main(String[] args) {
          Scanner in = new Scanner(System. in );
          System.out.printf("Please enter the name of the input file: ");
          String input_name = in .next();
          System.out.printf("Please enter the name of the output CSV file: ");
          String csv_name = in .next();
          System.out.printf("Please enter the name of the output pretty-print file: ");
          String pretty_name = in .next();
          process_grades(input_name, csv_name, pretty_name);
          System.out.printf("\n\nExiting...\n");
      }
  }
  </code></pre>