Question

I have the method below, which passes data into an object which is also included. If I debug the program step by step, then I can see that the correct data is being passed into each field in the object. However, once I try and do a println, I'm not getting the right data back from the object

for each iteration of the loop, The data from the scrape is passed in to the object as:

[Prague, Aer, Lingus, EI645, 24, Feb, 17:15, Arrived, 17:32]

and is assigned to all the fields without a problem. when I call the print method, I then get the following, instead of one of the above rows

====================TEST=======================
com.beezer.DublinAirportArrivals.ArrivalDetails@25595f51
================END TEST=======================

What am I doing wrong here?

public HtmlParser(Properties config) throws IOException{

    url = config.getProperty("url");
    airline = config.getProperty("airline");

    print("Fetching.........%s" , url);

}

public ArrayList<ArrivalDetails> process() throws IOException{
    Document doc = Jsoup.connect(url).get();

    Elements tableRow = doc.getElementsByTag("tr");

    for(Element tr : tableRow){
        if(tr.text().contains(airline)){
            if(tr.text().contains("Arrived")){
            String delims = "[ ]+";
            String[] singleRowArray = tr.text().split(delims);
            ArrivalDetails temp = new ArrivalDetails(singleRowArray);
            capture.add(temp);
            }
        }

    }
    testPrint();
    return capture;
}

public static void testPrint(){
    System.out.println("====================TEST=======================");
    System.out.println(capture.get(capture.size()-1));
    System.out.println("================END TEST=======================");
}

Here's my other Class

public class ArrivalDetails {

    public ArrivalDetails(String[] singleRowArray) {
        String origin = singleRowArray[0];
        String airline1 = singleRowArray[1];
        String airline2 = singleRowArray[2];
        String flightNo = singleRowArray[3];
        String date = singleRowArray[4];
        String month = singleRowArray[5];
        String ArrTime = singleRowArray[6];
        String status = singleRowArray[7];

    }

}
Was it helpful?

Solution

You need to add a toString() method to your class. This says what you want to display when you print an object of this type. Here is an example that you could add to your class that you can modify.

@Override
public String toString() {
    return origin + " " + date;
}

Be aware however that as your class is written now, none of your variables are accessible. You create them in the constructor and they are only visible/useable in the constructor. You need to make them class variables. You can change it like this

public class ArrivalDetails {

    String origin;
    String airline1;

    public ArrivalDetails(String[] singleRowArray) {
        this.origin = singleRowArray[0];
        this.airline1 = singleRowArray[1];
    }
}

Check out this other question on StackOverflow for more info. How to use toString() in Java

OTHER TIPS

You saw the results of the toString() method on the Object class, and you haven't overridden it in ArrivalDetails.

this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode()) 

Override it so you control the output of an instance of that class. In that method, you construct and return the String that will be passed to System.out.println.

The reason toString() is called is because of String Conversion, covered by the JLS Section 5.1.11:

[T]he conversion is performed as if by an invocation of the toString method of the referenced object with no arguments; but if the result of invoking the toString method is null, then the string "null" is used instead.

Extending the other answers with some "glue explanation",

System.out.println(Object o);

prints the result of

o.toString()

and as you haven't overridden toString, the original implementation of Object#toString applies.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top