java - How to handle reading from files where lines aren't well-formed -


i have directory several text files in it, each of contains line of text delimited commas. expecting find 4 tokens read each file while creating string array. so, normal line in text file this:

cat,dog,876358293472,884459654596 

but want account files not formed expect. or if file empty. instance, file might have this:

cat,dog, 

or

cat,0000000000000 

i have code handle token length not sure how account cases file isn't formatted expect. here's have far:

while((line = br.readline()) != null) {         try {             string [] tokens = line.trim().split(",");             if (tokens.length != 4) {                 return null;                 } 

are there other checks should in addition 'token.length'?

you need call split(",",-1), prevent empty fields merging:

"a,b,c,".split(",") --> ["a", "b", "c"]  "a,b,c,".split(",",-1) --> ["a", "b", "c", ""] 

if care getting 4 strings, test fine.