I parse all TD elements of a webpage.
There is 1 TD element which seems to be empty.
EUR 1,00
EUR 2,00
empty <--
EUR 3,00
How can i fix this and remove the empty item from arraylist (results)?
Thank you in advance.
My code:
try {
Document doc = Jsoup.parse(kpo);
// Get all td's
Elements waardes = doc.select("TBODY");
Iterator<Element> postIt = waardes.select("td").iterator();
// Iterator over those elements
//ListIterator<Element> postIt = waardes.listIterator();
postIt.next();
postIt.next();
postIt.next();
postIt.next();
SearchResults sr1 = new SearchResults();
//SearchResults sr2 = new SearchResults();
while (postIt.hasNext()) {
sr1 = new SearchResults();
// Add the game text to the ArrayList
sr1.setNaam(postIt.next().text());
sr1.setWaarde(postIt.next().text());
results.add(sr1);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return results;
}
Edit:
I got it to work like:
while (postIt.hasNext()) {
String naam = postIt.next().text();
String waarde = postIt.next().text();
sr1 = new SearchResults();
// Add the text to the ArrayList
if (!naam.trim().isEmpty() && !waarde.trim().isEmpty()) {
sr1.setNaam(naam);
sr1.setWaarde(waarde);
results.add(sr1);
}
Just make sure that you already don’t add the
SearchResultswhen thewaardeis empty. This way you don’t need to fiddle afterwards with cleaning the one and other.So, replace
by for example