How can I read any type of file from a location and access only a particular column to perform some operations on it. Some rows in my column may contain null values too.
Here’s an example:
name | age | sex
xyz | 22 | F
yyy | 25 | M
zzz | | F
aaa | 22 | M
I want to access the column age so that I can perform some operations on it, like calculate age group, or performing some other task(s). I have written the code for this function but I still need to access the column to perform this.
I’m pretty new at file reading, hence I need help. I need to do this in java.
I have tried the following:
while (s.hasNextLine()) {
String line = s.nextLine();
System.out.println(line+"line ?????");
String[] cols = line.split("|");
System.out.println("lets see column only=="+cols[2]);
}
Yet my output is only the 2nd character of the 1st column.
This doesn’t work since
splittakes a regular expression, and|is a special character when interpreted as a regular expression.Instead, try