-1

I'm writing a java program in Eclipse that runs gradle and unit tests. These tests output a bunch of information to the console. When a certain line comes up in the output I want to execute a certain method.

I would like to know if there is a way that I can scan the console output for a specific string, and when that string is found to execute my next method.

Basically I want an IF-THEN statement, while my program runs, that says if(ConsoleString==TargetString), do(This).

Hunter Turner
  • 6,804
  • 11
  • 41
  • 56
Kevin
  • 1
  • 2

2 Answers2

0
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
if (input.equals("TargetString")) {
    doSomething();
} else {
    doSomethingElse();
}
noscreenname
  • 3,314
  • 22
  • 30
  • While code often speaks for itself, it's good to add some explanation to your code. This popped up in the review queue, as code-only answers tend to. – Will Jul 26 '16 at 21:08
0

There is a good answer to this here: Redirect console output to String

I like the ConsoleOutputCapturer class by @Manasjyoti Sharma in the answer.

Community
  • 1
  • 1
ghg565
  • 422
  • 6
  • 15