I am using the following code to extract all alt tags from an html file:

Elements imgs =  doc.select("img");

                    for (Element alts : imgs) {

                        String altText = alts.attr("alt");


                        if(!(alts.attr("alt").equals(null)))
                            System.out.println("alt tags: " + alts.attr("alt"));
                        //getGui().setTextVers("\r\n"  + ">\r\n" + altText + "\r\n" );

                    }

I would like to ignore the alt tags which are blank and just print out the tags that contain text. I am using an if statement to test for empty alt tags but it still prints out all alt tags even though some contain text. Could someone help please?

Thanks

有帮助吗?

解决方案

Maybe change your if statement to be:

if ((altText != null) && !altText.isEmpty())
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top