Reputation: 97
Something strange happens on a text parser that I am writing an I thought maybe you experts can find what I am doing wrong.
The parser is searching for several search-terms in the text and copy the found results to an output parsed file on the SD card.
The code is:
String line; // line reading buffer
…
Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0]));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
…
while ((line = bufferedReader.readLine()) != null){
line = bufferedReader.readLine(); // reading the buffer
/*** parsing the buffer line by line ***/
if (line.contains("SearchTermA")){
// parsing the text in this line
}
if (line.contains("SearchTermB")){
// parsing the text in this line
}
// Check that we got to a cretin part in the file
if (line.contains("SearchTerm_text")){
textID = 1;
}
if (textID == 1){
if (line.contains("SearchTermC")){
// parsing the text in this line
}
}
Now, the issue is that at the beginning of the file (the file is very long) this works OK but sometimes along the way SearchTermB appears in the original text but is not deceted by the code. I tried stepping the code with Eclipse on the target android machin and I can clearly see that “line” contains SerchTermB but the debugger ignores this IF statement and continue to the next IF statement.
Could it be that line.containes() is missing a search-term?
Please help in finding what am I doing wrong since this prevents me from sleeping at night…..
Thanks,
Upvotes: 1
Views: 43
Reputation: 48871
Your while
loop is reading two lines at a time...
while ((line = bufferedReader.readLine()) != null){
line = bufferedReader.readLine(); // reading the buffer
In other words you call readLine()
in your while
statement in order to check that it's not a null
result and then you immediately call readLine()
again.
Get rid of this...
line = bufferedReader.readLine(); // reading the buffer
...and see what happens.
Upvotes: 2