Dustin Schreader wrote:Any idea how to get the array to contain every row of the CSV file or maybe a more simple way to accomplish this?
Dustin Schreader wrote:I got it!
Dustin Schreader wrote:My goal is to read in from CSV file and then add a new item to it and then write all the info back to the CSV file. That way it looks like I just added something new to the CSV file. I suppose I should have mentioned that before, would there be an easier way of doing this?
Winston Gutkowski wrote:Well if you need to add a new element to a line (unless it's only at the beginning or end) you're likely to have to split each line into elements, and that means either making it a String or a List<String>, as I suggested. The advantage of a List is that you don't have to worry about resizing; the class (eg, ArrayList) does it for you.
If you may also need to insert an entire row, then you're probably going to need some way of storing the file contents as a matrix.
Again, you have two choices:
String (a two-dimensional String array) or a List<List<String>> (as I described above); and again, the advantage of using a List is that you don't need to worry about the resizing - although I admit, it does look a bit more verbose.
Joanne Neal wrote:Actually you should create a Student class with a constructor that accepts the CSV line as a parameter...
Joanne Neal wrote:
Note also that your method of splitting up the CSV line won't work if any of the elements contain commas. You are better off using a third party CSV library that will handle things like this.
Winston Gutkowski wrote:
Well done, but I'm afraid it's redundant.
If you look at the documentation for BufferedReader.readline(), you'll see that it already returns "the contents of the line, not including any line-termination characters".
dataArray = dataRow.split("\n");
will return exactly 1 element, so
rows[x] = dataArray;
is exactly the same thing as
rows[x] = dataRow;
Since, in your original code, you were splitting the line, I assumed that you needed to store all the CSV elements separately. Apologies for any confusion.