Issue
I cannot convert the CSV file I read into a list. What I want to do is create a List<List<Double>>
list. I am creating the same list as below but as List<String>
.
-0.140625000000000,0.986816000000000,-0.209473000000000
-0.144531000000000,0.959473000000000,-0.262207000000000
-0.168945000000000,0.945312000000000,-0.340820000000000
-0.141602000000000,0.939453000000000,-0.289551000000000
-0.145508000000000,0.950195000000000,-0.305664000000000
-0.147461000000000,0.946777000000000,-0.302246000000000
-0.146484000000000,0.950684000000000,-0.305176000000000
-0.145996000000000,0.951660000000000,-0.305176000000000
....
For Example
List<List<double>> list = [
[-0.447266000000000, 0.417969000000000, 0.738770000000000],
[-0.447266000000000, 0.417969000000000, 0.738770000000000],
[-0.447266000000000, 0.417969000000000, 0.738770000000000],
[-0.447266000000000, 0.417969000000000, 0.738770000000000],
[-0.447266000000000, 0.417969000000000, 0.738770000000000]
]
My code
@RequiresApi(api = Build.VERSION_CODES.N)
public static void main(String[] args) throws IOException, CsvException {
String fileName = ".../app/src/main/assets/ahmetozkilic.csv";
try (CSVReader reader = new CSVReader(new FileReader(fileName))) {
List<String[]> r = reader.readAll();
List<String> myList = new ArrayList<>();
r.forEach(x -> myList.add(Arrays.toString(x)));
System.out.println(myList);
}
}
Solution
Try this :
String fileName =
".../app/src/main/assets/ahmetozkilic.csv";
try (CSVReader reader = new CSVReader(new FileReader(fileName))) {
List<String[]> r = reader.readAll();
List<List<Double>> myList = new ArrayList<>();
r.forEach(lineArr -> {
List<Double> line = new ArrayList<>();
for (String str : lineArr) {
line.add(Double.parseDouble(str));
}
myList.add(line);
});
System.out.println(myList);
}
Answered By - Abdo21
Answer Checked By - Candace Johnson (JavaFixing Volunteer)