I have a section of code like this:
while (scanFile.hasNextLine())
{
String currentLine = scanFile.nextLine();
if (currentLine.isEmpty())
{
System.out.println();
continue;
}
String[] allWordsInCurrentLine = currentLine.split(" ");
I then have three separate methods I want to use to manipulate the file being scanned.
The first method scans each line of the code individually and prints out some form of output (pigLatin) line by line (so one line of output is given for each iteration of the “while” loop.
The second method scan all the text first and stores various information in different arrays and variables. The user can then search this information (so the entire “while” loop must be satisfied before any output can be given).
I then have a third method which does something similar to the first one.
I am trying to find the most efficient way to reuse this section of code. Originally, I tried just doing method calls underneath the bottom line of the code above and have a for loop at the top of it, so if a==0, call first method, then second time through while loop, a will be incremented and second method will be called and so on, like this:
for(String currentWordInCurrentLine : allWordsInCurrentLine)
{
if (i==0)
pigLatin(currentWordInCurrentLine); // Part 1
if (i==1)
searchForWord(currentWordInCurrentLine, currentLine); // Part 2
if (i==2)
brailleTranslator();
}
There are a few issues with this, not the least the fact it looks awful. Someone advised me to try interfaces, but they are a bit above my station (am only a few weeks into my course). Does anyone have any other suggestions?
Edit: The pigLatin method call works fine using the above (albeit horrible looking) way. However, I can’t call the next method like that, as it needs to run through the whole ‘while’ loop before it can operate.
I would wrap the functionality you want with your three different processes in their own custom callable-like class, and then pass them to the code above. In this case the shared code would look something like this:
Then you can share the functionality of that while loop across
FileHandler. What is aFileHandler? I would define it very simply, like this:Your first one might look like this:
And finally, to run it, you would do something like this
EDIT
I just want to point out that there is an “easier”, less object-oriented way; you could have a method take a file and return a String[][], which is a two-dimensional array of file lines by words in those lines. Then you can pass around that array to your handlers.
However the downside with this approach is that it breaks on large files (you have to load the whole thing into memory at once) and you don’t get any output until the entire file has been read. A (maybe non-obvious) advantage of my answer is that you “stream” the file; you process a line with all of the handlers, and then throw out the line, allowing java to reuse that memory. This code would let you handle a 10 GB file while only using java memory equal to the length of the longest line in that file (plus any state that your IndexHandler is saving).