Colonel Mustard
Colonel Mustard

Reputation: 1533

Using List<String>

I've created a program to scrape data from a website using the <tr> tag. When I run my program and print my array List<String> getDataList I get a single string row of data as a printout.

[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,

So basically, I want to format the data into a useable format and make it readable. As you can see each comma separates one flight from another, but I need to separate themselves and add text inbetween, like the following. (I use code for the text i want to add manually)

FR 9083 From: Bournemouth To: Alicante Dep:10:10 Arr:13:35 Status Landed 19:00 and so on.......

How do i go about separating the data in order to add text elements in the middle.

I've thought of using the spaces to do it, but some destinations like 'London Stanstead' have spaces in the names as well as just the separators.

Can someone tell me how I would go about doing this please? I'm learning as i go here......\

Many Thanks

Upvotes: 0

Views: 98

Answers (3)

Sharp Edge
Sharp Edge

Reputation: 4192

Well since you're using List interface Its probably has assigned an ArrayList to it.

So first of all you gotta convert it to String.

String unSort = getDataList.toString();
String Sort[] = unSort.split(",");
String noComma = Sort.toString();

This will solve your comma seperation problem, you can further split the sorted text by using some other regexes like ":" or any other.

String noColon[] = noComma.split(":"); 

Upvotes: 1

christopher
christopher

Reputation: 27346

This is the interesting part of the String.

FLIGHT FROM TO DEPART ARRIVE STATUS, FR 9083 Bournemouth Alicante 10:10 13:35 Landed 19:00,

And to me it seems like a job for a HashMap wrapped up in an Object.

Example

private Map<String, String> fields;

public Flight(String input)
{
    // Input is currently equal to the value above.
    fields = new HashMap<String, String>();
    parseInput();
}

private void parseInput(String input)
{
    // First split up each section.

    String[] tokens = input.split(",");

    // Next, split the headers up.
    String[] headers = tokens[0].split(" ");
    // And the values.
    String[] values = tokens[1].split(" ");

    for(int x = 0; x < headers.length; x++)
    {
         // Map each header to it's respective value.
         fields.put(headers[x], values[x]);
    }
}

public String toString()
{
    // Output the object as a string.
    StringBuilder builder = new StringBuilder();

    for(String key : fields.getKeySet())
    {
        builder.append(key + ":" + fields.get(key) + " ");
    }

    return builder.toString();
}

With this code, you pass in the String, starting with the field headers, and you call the toString method on the resulting Flight object, and it'll output your String in the desired format.

Upvotes: 0

gasparms
gasparms

Reputation: 3354

Take a look Google Guava Splitter. It's a useful library in these cases.

http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/base/Splitter.html

Upvotes: 3

Related Questions