1

I have an HTML that looks something like this

<div class="copy>
  <p>First tip</p>
  <p><span style="FONT-SIZE:medium"><br/></span></p>
  <p>Second Tip</p>
  <p>&nbsp;</p>
  <p>Third tip</p>
  <p>&nbsp;</p>
  <p>Fourth tip</p>
</div>

I'm using Jsoup to extract the text inside the p elements and store them in an arraylist. But I don't want to store the ones that don't have any text in them . I've tried the following but it doesn't work.

Elements tips = doc.select("div.copy > p");
    for(Element tip: tips) {
        if(tip.text() != "") {
            if(tip.text() != "&nbsp;") {
                tipsArray.add(tip.text());
            }
        }

    }

The code doesn't add the second p element with the br tag in the arraylist but it isn't working for &nbsp. I'm trying to use the arraylist on a listview in Android. I've also tried using \u00a0 instead of &nbsp inside the if statement but that doesn't work either.Is there another way of doing this or am I doing something wrong? I don't know if this problem is Java -related or Android-related. Thanks for any help.

Bidhan
  • 10,607
  • 3
  • 39
  • 50

1 Answers1

0

You should NOT use == operator to compare String's values. You must use equals method:

for(Element tip: tips) {
        if(!"".equals(tip.text())) {
            if(!"&nbsp;".equals(tip.text())) {
                tipsArray.add(tip.text());
            }
        }

You could also make sure to not add paragraphs containing multiple &nbsp; or white spaces:

if(!"".equals(tip.text().replace("&nbsp;", "").trim()))

Also take a look here : How do I compare strings in Java.

Community
  • 1
  • 1
Daniel
  • 1,861
  • 1
  • 15
  • 23