swp3h6
asked on
finding patterns in a file
I have this file that is being iterated through one line at a time. I'm trying to find cases where three lines occur in a specific order, meaning that if we have three cases, and they appear anywhere in the file as follows:
case_one
case_two
case_tree
a specific action is performed.
any ideas?
thanks.
case_one
case_two
case_tree
a specific action is performed.
any ideas?
thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
could you be a little more specific? I tried maintaining an array of booleans to keep track of patterns, and i am having similar problems with the stack. Wont there have to be a specific order in which these lines are tested? and wouldn't it also have to have conditional statements for what is on top of the stack? for example, as written above:
while(line != null) {
line = br.readLine();
if(line == case_one) {
stack.push();
}
if(line != case_two) {
stack.pop();
}
}
would not work, because that push would always be popped.
while(line != null) {
line = br.readLine();
if(line == case_one) {
stack.push();
}
if(line != case_two) {
stack.pop();
}
}
would not work, because that push would always be popped.
> would not work, because that push would always be popped.
It would always be popped if the desired case is not met, and that is the case. You only want to push if all of the three cases are met in order, one after the other, otherwise there is no need to keep on in the stack. It's the same principle as the one I posted with the only difference that I use three conditional statements and no stack.
It would always be popped if the desired case is not met, and that is the case. You only want to push if all of the three cases are met in order, one after the other, otherwise there is no need to keep on in the stack. It's the same principle as the one I posted with the only difference that I use three conditional statements and no stack.
public static boolean containsAllThreeLinesInOrd er(String s1, String s2, String s3, String fileName) {
BufferedReader in = null;
boolean found = false;
try {
in = new BufferedReader(new FileReader(fileName));
Stack s = new Stack();
String line = null;
while((line = in.readLine()) != null && found == false) {
switch(s.size()) {
case 0:
if (s1.equals(line)) {
s.push(line);
}
break;
case 1:
if (s2.equals(line)) {
s.push(line);
}
else {
s.clear();
}
break;
case 2:
if (s3.equals(line)) {
found = true;
}
break;
}
}
} catch(IOException e) {
e.printStackTrace();
}
finally {
try { in.close(); } catch(IOException e) { /*ignore */ }
}
return found;
}
BufferedReader in = null;
boolean found = false;
try {
in = new BufferedReader(new FileReader(fileName));
Stack s = new Stack();
String line = null;
while((line = in.readLine()) != null && found == false) {
switch(s.size()) {
case 0:
if (s1.equals(line)) {
s.push(line);
}
break;
case 1:
if (s2.equals(line)) {
s.push(line);
}
else {
s.clear();
}
break;
case 2:
if (s3.equals(line)) {
found = true;
}
break;
}
}
} catch(IOException e) {
e.printStackTrace();
}
finally {
try { in.close(); } catch(IOException e) { /*ignore */ }
}
return found;
}
:-)
Why the 'B' though?
in = readline...
while (in != null)
{
if (in.equals("line1"))
{
in = readline...
if (in.equals("line2"))
{
in = readline...
if (in.equals("line3"))
{
<do something>
}
}
}
}