0

I'm attempting to read in a CSV file with various data types.

A row of the sheet would be like below:

Single, Monthly, Accelerated, John, Smith, 08/15/1951, Yes

I then need to assign each field to a variable name, preform some calculations, print an output and then move onto the next line in the excel sheet

Up until now, I've been using the below

ArrayList<String> lines = new ArrayList<>();
try {
    BufferedReader reader = new BufferedReader(new FileReader(fileName));
    String line = null;
    while ((line = reader.readLine()) != null) {
        lines.add(line);
        }

But this creates an array with each slot containing a long string with the text (including comma) of the corresponding excel row. This seems inefficient and impractical as i then have to traverse each slot/string to extract the values.

Once I have the methodology, I wont have any issue writing the code but i don't know the best way to go about it

Is it better to read each cell separately and assign to a variable ? Or is it better to read in a file once and traverse it afterwards? Perhaps there is a more efficient way to do this task

Edit : I also though of attempting to read in the entire CSV file as a 2D array, but the different data types could be an issue..?

Kevin
  • 15
  • 2
  • 6

2 Answers2

0

You don't need to store the lines. Instead create a class that represents each row and add them to a list.

Something like :

class MyData
{ 
   String status;
   String salary;
   String accelerated;
   String firstName;
   String lastName;
   String date;
   String trueOrFalse;

}

You could keep them without any access qualifier or make them private and add getter/setters.

In your file reader, split the line using the separator used in csv which is by default a comma ( , )

ArrayList<MyData> datas = new ArrayList<MyData>();
while ((line = reader.readLine()) != null) {
        String[] columns = line.split( "," );
        MyData data = new MyData();
        data.status = columns[0];
        data.salary = columns[1];
        .
        .
        data.trueOrFalse = columns[6];
        datas.add( data );
        }

OR

If you want to just perform some calculations and print, you don't need a separate class or even ArrayList<String> lines.

Just do :

while ((line = reader.readLine()) != null) {
        String[] columns = line.split( "," );
        // Perform calculations with columns
        // Print.
}

No need to store information if that is the case.

SomeDude
  • 13,876
  • 5
  • 21
  • 44
0

You can try something similar to this. Use StringTokenizer to split the line by comma and add those elements to another List as strings in each iteration.

ArrayList<ArrayList<String>> lines = new ArrayList<>();
try {
    BufferedReader reader = new BufferedReader(new FileReader(fileName));
    String line = null;
    while ((line = reader.readLine()) != null) {
        ArrayList<String> tokens = new ArrayList<>();
        StringTokenizer st = new StringTokenizer(line, ",");
        while (st2.hasMoreElements()) {
            tokens.add(st2.nextElement());
        }
        lines.add(tokens);
    }
}

Now you can use proper casts to convert them to types you want. For example, to get the date,

DateFormat format = new SimpleDateFormat("mm/dd/yyyy", Locale.ENGLISH);
String dateString = lines.get(0).get(5);
Date date = format.parse(dateString);
Janith Jeewantha
  • 185
  • 2
  • 12
  • Thanks for this, managed to get it to work for me. Is there any particular reason you thought StringTokenizer was a good option ? the reason I ask is the java doc states: **StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead** – Kevin Jun 08 '17 at 15:15
  • You might want to look at [this question](https://stackoverflow.com/q/6983856/4310386). You can choose any method to do the splitting since there are many. It is up to you. – Janith Jeewantha Jun 09 '17 at 03:58