-1

I have a web pages scraper that grabs text using a tag from html "tr"

I can retrieve the data sucessfully, but I want to add it to either an array or to a string, because I need to select specific elements from it.

Currently the follwoing:

public HtmlParser(Properties configuration) throws IOException {

    String url = configuration.getProperty("url");
    Document doc = Jsoup.connect(url).get();
//  this.doc = doc;
    this.dataString = new String();
    Elements tableRows = doc.getElementsByTag("tr");

    for (Element tr : tableRows){
        dataString.add(tr.text());
    }

which will return the text I need, along the lines of:

[Flights on time 93%, Within 1 hour 99%, FLIGHT FROM TO DEPART ARRIVE STATUS, FR 9083 Bournemouth Alicante 10:10 13:35 Landed 19:00, FR 1902 Krakow Dublin 10:45 12:55 Estimated Arrival 23:05, ATC DELAY, FR 1402 Shannon Tenerife Sth 11:15 15:15 Estimated Arrival 19:53, OPERATING FROM SHANNON AIRPORT. PASSENGERS TO BE COACHED TO SHANNON AIRPORT.,

I want to return the above text into an array or string whichever applicable.

with the above text, I am getting a compile error saying

The method add(String) is undefined for the type String

when I try to add the data from each iteraton of the for loop to the string I'm getting the compile error above, and I've tried using .add and .append but it doesn't work. How do I fix this?

Also, my next step is to split the string by the operator "," and possibly a space.

If I split the text does it need to be into an array, or will a string will do? if so, how do I do it?

Im reasonably new to java and I'm learning as I go here, so any help anyone can give me would be greatly appreciated.

I'm also then looking to select any of the involved strings based on certain criteria like a keyword. How does one do this?

Colonel Mustard
  • 1,330
  • 15
  • 36

2 Answers2

1

You can use the append method of StringBuilder or StringBuffer

http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html

and if you want to which one is best to use then see this Difference between StringBuilder and StringBuffer

Community
  • 1
  • 1
Girish
  • 1,667
  • 1
  • 16
  • 28
0

I'm pretty sure you can just use StringBuilder class. That might work a little better. Here is a link to someone who asked something similar.

In Java, how to append a string more efficiently?

I'm not advanced enough to know for 100% if this is the problem because in SOME cases, I might not be right. But I'm pretty sure that exampleString.add only works with ArrayList but I might be wrong, at least in Java. I think you should try concatenating the string.

You can also try

exampleString.append

Hope that helps.

Community
  • 1
  • 1
user3251611
  • 11
  • 1
  • 5