I have a text file. Each line in the file represents a record having 'n' number of columns delimited by a | (pipe) character. The column-values are of type int, string, date, timestamp, etc. Empty string and spaces are also possible as column values.
I am validating only the count of the column-values and validation of data type is not required.
Sample valid records of 5 columns each:
1234|xyz|abc|2016-04-08 11:12:40|234
1235|efgh|abc|2016-04-09 11:25:40|
1236|efghij| ||
Validation code:
boolean valid = true;
String line = buffReader.readLine();
String[] tokens = null;
while (line != null){
tokens = line.split("\\|");
if ((tokens.length==4 || tokens.length==5) && countPipes(line)==4){
} else {
valid = false;
break;
}
line = buffReader.readLine();
}
private int countPipes(String line){
int count = 0;
count = line.length() - line.replace("|", "").length();
return count;
}
I feel that the code can be better. Can someone let know how i can improve this code?