I have a flat file that I’m trying to scrub for import into a database. The layout is inconsistent, but always consists of a fieldname (variable length text but ALWAYS one of 9 words or phrases) followed by a freeform text field (variable length up to 1024 bytes). I need to extract the 1024 byte field, and arrange them into columns across fieldnames.
Input File:
foo-01 bunches of data
foo bar01 more bunches of data including a bunch of notes
foo-01 lots of data lives in this field
foo18 monday notes
...etc.
Output File – delimited, spaces trimmed
foo-01;foo bar 01;foo18 (<-- header row)
bunches of data; more bunches of data including a bunch of notes; ;
lots of data lives in this field; ; notes
My strategy is this: Read each line. If the line starts with one of the nine fieldnames, I write a substring (first character after the fieldname through the last character in the line – spaces trimmed) to a deliminted flat file in the appropriate column position.
This code works,
if(inputLine.startsWith("foo-01"))
{
String lineVal = inputLine.trim();
int lVLen = lineVal.length();
String outVal = lineVal.substring(17,lVLen);
String outValTrim = outVal.trim();
System.out.println(evalVal+" "+inputLine+" "+outValTrim);
}
else
...etc...
But raises questions.
Consider:
String outValTrim = inputLine.trim().substring(17,inputLine.trim().length()).trim();
-
What is the maximum number of methods that I can use? e.g.,
foo =
Stringmethod1.Stringmethod2.StringMethod3() -
Is there a rule for the order of the methods in the statement?
-
What are the best practices for combining methods in one statement? I
feel it’s less human-readable, and I’m not sure about efficiency.
The maximum size for a method is 64K, you can’t use more method than this. You may also find that you don’t need to chain as many methods as you might think.
I suspect the first trim() is not correct…
What is best practice is to make the code are readable and easy to understand as possible.
Efficiency is rarely the most important issues and unless you can prove e.g. by using a profiler, that this is the cause of a problem for you, stick with readability.