Home > Enterprise >  How to compare String value to another string after using getText()?
How to compare String value to another string after using getText()?

Time:01-05

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");
}

CodePudding user response:

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");
}

CodePudding user response:

You might try the following:

int tempInCelsius = Integer.parseInt(getText().replaceAll("(?s).*?\\b(\\d )c\\b.*", "$1"));
  • Related