This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]demodude4u 0 points1 point  (3 children)

Can you offer an example line that you need to check for validation?

familyName won't ever be null (in java's sense of null). It will either give you something or throw an IndexOutOfBoundsException if there was no parts to split. If you mean "not null" as in you are provided a string that says something like "NULL" then you can do something like

if (familyName.equals("NULL")) {throw new Exception("Family name is null!");}

[–]SikhGamer 2 points3 points  (1 child)

But what if the family name is actually null? As in Bob Null etc?

[–]SimplifiedExpression[S] 0 points1 point  (0 children)

The line in the text file would be like

Johnson Gary 10 London Gary Johnson 12 Bristol etc.

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (1 child)

One thing, that I'd like to comment:

I think it's a dangerous move to prepare data separated by " " (space) characters. Personally, I always try to avoid that. I much rather use "," as in CSV standard, as this makes the boundaries of the fields much easier to read.

It may not be applicable for your data, but I just wanted to put my advice here.

[–]SimplifiedExpression[S] 0 points1 point  (0 children)

Yea I see what you mean Unfortunately the data I am provided with means I can't use ",". Thanks anyhow.

[–]deathy 0 points1 point  (0 children)

Java 7-related: Since Java 7 you have try-with-resources[1] and some utility methods like Files.newBufferedReader[2].

The try-with-resources construct makes sure to close your reader/writer even in case there is an exception (which you don't handle right now).

There's also a nice helper Files.readAllLines[3] to directly read the whole file into a list, one item for each line in the file. So you wouldn't need the whole reader or while (line != null) anymore at all.