I am a new bee in Java and want to map CSV columns according to their values. My orignal csv data is like:
name,salary,address
AAA,1000,kkk
BBB,880,lll
AAA,90,kkk
CCC,700,mmm
BBB,700,lll
Expected output should be in Hashmap Key Value pair of two columns name and salary like:
Key: AAA Value 1090
Key: BBB Value 1580
Key: CCC Value 700
This is my code:
String line = "";
InputStreamReader inStream = new
InputStreamReader(uc.getInputStream());
buff = new BufferedReader(inStream);
try {
while ((line = b.readLine()) != null) {
String[] fields = parseCsvLine(line);
// I dont know what to do here
}
b.close();
Log.d("Get data from API", "Processing Stop");
} catch (IOException e) {
e.printStackTrace();
}
public String[] parseCsvLine(String line) {
// Create a pattern to match breaks
Pattern p =
Pattern.compile(",(?=([^\"]*\"[^\"]*\")*(?![^\"]*\"))");
// Split input with the pattern
String[] fields = p.split(line);
for (int i = 0; i < fields.length; i++) {
// Get rid of residual double quotes
fields[i] = fields[i].replace("\"", "");
}
return fields;
}