1

I have this code, it gets the average grade, but I need to export the arraylist Hell to a CSV file. How do I do this?

import java.util.*;
import java.io.*;
import java.io.PrintWriter;
import java.text.*;
public class hello3 {
    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();

    processGrades(input_name, csv_name, pretty_name);
    System.out.printf("\nExiting...\n");
  }
      public static void processGrades (String input_name, String csv_name, String pretty_name)
  {
      PrintWriter csv = null;
      PrintWriter pretty = null;
      String[][] data = readSpreadsheet(input_name);

      boolean resultb = sanityCheck(data);
      int length = data.length;
      ArrayList<String> test_avg = new ArrayList<String>();
      ArrayList<String> HW_avg = new ArrayList<String>();
      ArrayList<String> NAME = new ArrayList<String>();
      ArrayList<String> ColN = new ArrayList<String>();
      ArrayList<String> Hell = new ArrayList<String>();
      for(int row = 1; row<length; row++)
      {
          String name = data[row][0];
          String name2 = data[row][1];
          String Name = name+" "+name2;
          int test1 = Integer.parseInt(data[row][2]);
          int test2 = Integer.parseInt(data[row][3]);
          int test3 = Integer.parseInt(data[row][4]);
          int Test = (test1+test2+test3)/3;
          String Testav = Integer.toString(Test);
          int hw1 = Integer.parseInt(data[row][5]);
          int hw2 = Integer.parseInt(data[row][6]);
          int hw3 = Integer.parseInt(data[row][7]);
          int hw4 = Integer.parseInt(data[row][8]);
          int hw5 = Integer.parseInt(data[row][9]);
          int hw6 = Integer.parseInt(data[row][10]);
          int hw7 = Integer.parseInt(data[row][11]);
          int HW = (hw1+hw2+hw3+hw4+hw5+hw6+hw7)/7;
          int[] trying = {Test, HW};
          int low = find_min(trying);
          String grade = null;
          if(low>=90)
          {
              grade ="A";
          }
          if(low < 90&& low>= 80)
          {
           grade = "B";   
          }
          if(low <80&&low>=70)
          {
              grade ="C";
          }
          if(low<70&&low>=60)
          {
              grade="D";
          }
          if(low<60)
          {
              grade = "F";
          }
          String Lows = Integer.toString(low);
          String HWav = Integer.toString(HW);
          test_avg.add(Testav);
          HW_avg.add(HWav);
          NAME.add(Name);
          Hell.add(Name);
          Hell.add(Testav);
          Hell.add(HWav);
          Hell.add(Lows);
          Hell.add(grade);

          System.out.println(Hell);
          System.out.printf("\n");


      }
  }


      public static int find_min(int[] values)
      {
          int result = values[0];
          for(int i = 0; i<values.length; i++)
          {
              if(values[i]<result)
              {
                  result = values[i];
              }
          }
          return result;
      }
  public static boolean sanityCheck(String[][] data)
  {
      if (data == null)
              {
                  System.out.printf("Sanity check: nul data\n");
                  return false;
              }
      if(data.length<3)
      {
          System.out.printf("Sanity check: %d rows\n",data.length);
          return false;
      }
      int cols= data[0].length;
      for(int row = 0; row<data.length; row++)
      {
          int current_cols = data[row].length;
          if(current_cols!=cols)
          {
              System.out.printf("Sanity Check: %d columns at rows%d\n", current_cols, row);
              return false;
          }
      }

      return true;
  }
  public static String[][] readSpreadsheet(String filename)
  {
    ArrayList<String> lines = readFile(filename);
    if (lines == null)
    {
      return null;
    }

    int rows = lines.size();
    String[][] result = new String[rows][];

    for (int i = 0; i < rows; i++)
    {
      String line = lines.get(i);
      String[] values = line.split(",");
      result[i] = values;
    }

    return result;
  }

  public static ArrayList<String> readFile(String filename)
  {
    File temp = new File(filename);
    Scanner input_file;

    try
    {
      input_file = new Scanner(temp);
    } catch (Exception e)
    {
      System.out.printf("Failed to open file %s\n",
              filename);
      return null;
    }

    ArrayList<String> result = new ArrayList<String>();
    while (input_file.hasNextLine())
    {
      String line = input_file.nextLine();
      result.add(line);
    }

    input_file.close();
    return result;
  }
}

Any help would be appreciated. thank you.

2
  • Do you mean CVS or CSV? Commented Dec 2, 2016 at 21:05
  • CSV, sorry my fingers were going to quick Commented Dec 2, 2016 at 21:07

1 Answer 1

2

Broadly, you need to open a file with the name you require, and a writer in a loop - like this:

File csvFile = new File(csvName);
try (PrintWriter csvWriter = new PrintWriter(new FileWriter(csvFile));){
  for(String item : list){
    csvWriter.println(item);
  }
} catch (IOException e) {
    //Handle exception
    e.printStackTrace();
}

Obviously you will have to print some commas as required

Sign up to request clarification or add additional context in comments.

1 Comment

No worries, feel free to accept the answer, if it helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.