0

I have successfully added Csv reading functionality to my application. The setup that work is I have a base class called CSVReader. For each csv file, I create A new Class and it extends from CSVReader. The setup I am currently working on is to have only one class that can be used for any csv file. SO far my code seems to make sense, but clearly is not working therefore I need to fix something.

Here is the base CSVReader package com.kbs.utilities.csvReader;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class CSVReader {
public ArrayList<String[]> readCsvFile(String filePath, boolean headerExists) { //Transfers info from CsvFile to an arraylist of string arrays

        String line = "";
        BufferedReader br = null;

        ArrayList<String[]> arrayClone = new ArrayList<String[]>(); //Container to store contents of String array

        try {
            FileReader fileReader = new FileReader(filePath); //creates file reader object in the specified csv FIle
            br = new BufferedReader(fileReader);

            if(headerExists) { //if CSVFile has header then read the first line and put it into a variable
                String headerLine = br.readLine(); 
                headerLine.chars(); //pointless line of code, just here to remove the warning of unused variable
            }

            while((line = br.readLine())!= null) {
                String[] currentRow =  line.split(","); 
                arrayClone.add(currentRow);
            }

        } catch (FileNotFoundException e) {
            // Error handling statement for throwing exception in case File isn't found
            e.printStackTrace();

        } catch(IOException e) {
            //// Error handling statement for throwing exception in case File can't be accessed using BufferedReader object
            e.printStackTrace();
        }

        finally {
            if(br != null){

                try {
                    br.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return arrayClone;
    }
}

This is the Class that extends CSVReader and is used to initiate the data from the specified csv

public class InitializeCSV extends CSVReader{
public ArrayList<Object> initCSV() {

    String csvFile = "C:\\Users\\kamalu\\Desktop\\Developer\\misc\\sandbox\\kbs-petshop\\trunk\\WebContent\\WEB-INF\\data\\"+csvFileName+".csv";

    boolean header = true;

    ArrayList<String[]> csvStringArray = readCsvFile(csvFile, header); /

    ArrayList<Object> list = new ArrayList<Object>();
    for(String[] iteration : csvStringArray) {

        Object[] iterationCopy = (Object[]) new Object();//Initializing using the array[] constructor

        list.add(iterationCopy);
    }
    return list;
}

}

micah kam
  • 21
  • 5
  • *"(Object[]) new Object();//Initializing using the array[] constructor"* No, "using array[] constructor" would be e.g. `new Object[42]`. – Andreas Apr 15 '20 at 05:33
  • Does this answer your question? [Creating an array of objects in Java](https://stackoverflow.com/q/5364278/5221149) – Andreas Apr 15 '20 at 05:35
  • the first solution was enough. now i just have to figure out how to softcode it – micah kam Apr 15 '20 at 15:51

0 Answers0