I am trying to ping and take the ping summary to be listed out in Matcher.group(0)
. The ping summary result which shown is:
Matcher.group(0)
time=35ms
time=3ms
time=2ms
time=3ms
time=2ms
time=83ms
time=3ms
time=69ms
time=2ms
time=5ms
java.io.IOException: The pipe is being closed
BUILD SUCCESSFUL (total time: 9 seconds)
Can I / How can I comparing these results? What I mean is, can I do something like:
if(m.group(0) >= "time=66ms") {
//do something
} else if (m.group(0) < "time=66ms") {
//do something
}
You'll want to capture just the number of milliseconds. Where you have something like time=\d+ms
in the regexp, use time=(\d+)ms
instead. Then you can use matcher.group(1)
to get that string, and use Integer.parseInt
to get it as an int.
int ms = Integer.parseInt(matcher.group(1));
if (ms >= 66) {
// do something
}
else {
// do something else
}
Note that you don't need the else if
, just an else
is fine. If an int isn't >= someValue
, then it has to to be < someValue
-- you don't need to check that tautology in the code. By the way, I would put that number 66 into a constant somewhere:
private static final int TIMEOUT_THRESHOLD = 66; // or some better name
...
int ms = Integer.parseInt(matcher.group(1));
if (ms >= TIMEOUT_THRESHOLD) ...
That way it'll be easy to change it later, if you need to, without searching everywhere through your code.