Reputation: 67
I try to readout a csv-File. In my Code I readout the Lines in the csv-File. Therefor I use an arraylist to add each item in the line to the arraylist.
After reading out a line i storte the array list in an other array list and then go to read out the next line and so on and ao on.
Now I have here a few Questions and one Problem.
1) When I add an item to an arraylist is it always added at the en of the list (When I give no specific place where to add the item like in my code)? With this then the order of the csv-file would be keeped.
2) I figuered out, that when I clear the arraylist datatemp to prepare the reading of the next line, also the content of the array list data is cleared. How can I prevent my code from doing so?
3) Is there a way to make an normal array out of the arraylist data I have?
4) Is my way of reading out the csv-File correct or is there any better way?
public class CSVReader {
public void ReadCSV (String csvfilepath) {
BufferedReader CSVFile = null;
String dataRow = null;
ArrayList<Float> datatemp = new ArrayList<Float>();
ArrayList<ArrayList<Float>> data = new ArrayList<ArrayList<Float>>();
try {
CSVFile = new BufferedReader(new FileReader(csvfilepath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
dataRow = CSVFile.readLine();
} catch (IOException e) {
e.printStackTrace();
}
while (dataRow != null){
String[] dataArray = dataRow.split(",");
for (String item:dataArray) {
datatemp.add(Float.valueOf(item));
}
try {
dataRow = CSVFile.readLine();
} catch (IOException e) {
e.printStackTrace();
}
data.add(datatemp);
datatemp.clear();
}
try {
CSVFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Upvotes: 2
Views: 7132
Reputation: 1492
Ad 1. Have you read ArrayList reference? Method add() "Appends the specified element to the end of this list."
Ad 2. Seems like you don't get how the reference work yet. Just declare datatemp inside the loop and don't clear it and it should work ok.
Ad 3. Read the class reference please. There is a method to array. Use it like that in our example:
Float[] array = data.toArray(new Float[data.size()])
Ad 4. If this is not a school assignment then use a library. Check this thread: CSV API for Java
PS: Please use docs from Oracle, all your answers were here: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
Upvotes: 1
Reputation: 244
You add datatemp to the data array but then you clear the array. it will clear the data in data as well. you need to copy the array like this:
data.add(new ArrayList<Float>( datatemp ) );
I do recommend using a open source csv library and not doing the parsing yourself.
Upvotes: 0
Reputation: 328598
1) With this then the order of the csv-file would be keeped.
Yes
2) How can I prevent my code from doing so?
You need to create a new tempArrayList each time:
while (dataRow != null){
datatemp = new ArrayList<Float>();
3) Is there a way to make an normal array out of the arraylist data I have?
You can create an array of arraylists:
ArrayList[] array = data.toArray(new ArrayList[data.size()]);
You can do then it recursively to create a 2D array, but I would stick to the list of list which is easier to handle.
4) Is my way of reading out the csv-File correct or is there any better way?
Use a library, there are many of them, which has been tested. It is much more difficult to parse a CSV file correctly than it seems at first sight.
Upvotes: 2
Reputation: 135992
Without looking into details, why not http://opencsv.sourceforge.net/ ? Used it myself, nice thing, trust me
Upvotes: 0