-2

When I extract tooltiptext using getText() method, output comes in three lines:

outside 
temperature
20c

But how to compare extracted 20c to another string 30c?

My approach:

if (tooltiptext.getText() > 30c) {
    System.out.println("temperature is too low outside");
} else {
    System.out.println("temperature is hot outside");
}
Turing85
  • 18,217
  • 7
  • 33
  • 58
Jackie M
  • 65
  • 7

2 Answers2

1

This should do it:

String tooltipText = "outside\ntemperature\n20c";
// ensure that we parse only the last line, so if digits appear in the other lines, there is no impact
String toolipTempText = tooltipText.split("\n")[2];
int tempInCelsius = Integer.parseInt(toolipTempText.replaceAll("[^0-9]", ""));
if (tempInCelsius > 30) {
  System.out.println("temperature is too low outside");
} else {
  System.out.println("temperature is too hot outside");
}
0

You might try the following:

int tempInCelsius = Integer.parseInt(getText().replaceAll("(?s).*?\\b(\\d+)c\\b.*", "$1"));
g00se
  • 3,207
  • 2
  • 5
  • 9