-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCsvReader.java
More file actions
33 lines (28 loc) · 1.06 KB
/
Copy pathCsvReader.java
File metadata and controls
33 lines (28 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class CsvReader {
public static ChartData readDataFromCSV(String filePath) throws IOException {
ArrayList<String> xData = new ArrayList<>();
ArrayList<String> yData = new ArrayList<>();
String xLabel = "";
String yLabel = "";
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) {
String line;
if ((line = bufferedReader.readLine()) != null) {
String[] headers = line.split(",");
xLabel = headers[0].trim();
yLabel = headers[1].trim();
}
while ((line = bufferedReader.readLine()) != null) {
String[] values = line.split(",");
if (values.length >= 2) {
xData.add(values[0].trim());
yData.add(values[1].trim());
}
}
}
return new ChartData(xData, yData, xLabel, yLabel);
}
}