0

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];

    }

}
chryss
  • 7,459
  • 37
  • 46
Colonel Mustard
  • 1,482
  • 2
  • 17
  • 42
  • You haven't overridden the `toString()` method of your `ArrivalDetails` class. – takendarkk Feb 24 '14 at 21:22
  • can you explain that to me please? – Colonel Mustard Feb 24 '14 at 21:23
  • possible duplicate of [Getting ExamQuestion@143c8b3 as print out in console](http://stackoverflow.com/questions/14743784/getting-examquestion143c8b3-as-print-out-in-console) – ApproachingDarknessFish Feb 24 '14 at 21:24
  • Java doesn't know what you want to display when you say `println(capture.get(capture.size()-1))`. Do you want it to print just the origin? Maybe just the date? You have to specify by writing a `toString()` method to say what you want to print. – takendarkk Feb 24 '14 at 21:25
  • @mcgowan.b Given this question, and your other questions, I urge you to spend some time with the very good free-of-cost online [Java Tutorial](http://docs.oracle.com/javase/tutorial/index.html) by Oracle. The bottom of [this page](http://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html) answers this question. – Basil Bourque Feb 25 '14 at 00:29

3 Answers3

1

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.

Community
  • 1
  • 1
rgettman
  • 176,041
  • 30
  • 275
  • 357
1

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

Community
  • 1
  • 1
takendarkk
  • 3,347
  • 8
  • 25
  • 37
  • Excellent. If I wanted to use the output that I have garnered here on the println as a string for something, how do i do it. Can i call the toString method and use the return string? – Colonel Mustard Feb 24 '14 at 21:44
  • If you mean something like `String s = myObject.toString()` yes you can do that. – takendarkk Feb 24 '14 at 21:48
0

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.

Smutje
  • 17,733
  • 4
  • 24
  • 41