-1

I have a directory with several text files in it, each of which contains a line of text that is delimited by commas. I am expecting to find 4 tokens as I read from each file while creating a String array. So, a normal line in the text file would be like this:

cat,dog,876358293472,884459654596

But I want to account for files that are not formed as I expect. Or if the file is empty. For instance, a file might have only this:

cat,dog,

or

cat,0000000000000

I have some code to handle the token length but am not sure how to account for cases where the file isn't formatted like I expect. Here's what I have so far:

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

Are there other checks I should do in addition to 'token.length'?

gppanter
  • 333
  • 1
  • 6
  • 19
  • 1
    That will depend on your requirements. There's no "universal rule" to check if "a line is formatted like I expect". You'll have to answer that to yourself: what do you expect? :- ) – ericbn Apr 27 '15 at 21:52
  • Ah yes, I do expect the second two tokens to be numbers. – gppanter Apr 27 '15 at 22:10
  • So you want `tokens[0]` and `tokens[1]` to have alpha characters, and `tokens[2]` and `tokens[3]` to have numeric ones? You might want to check [this](http://stackoverflow.com/questions/5238491/check-if-string-contains-only-letters) and [this](http://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-a-numeric-type-in-java). – ericbn Apr 27 '15 at 22:17

1 Answers1

1

You need to call split(",",-1), to prevent empty fields from merging:

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

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

If all you care about is getting 4 strings, then the test is fine.

Michael Bar-Sinai
  • 2,729
  • 20
  • 27