kivok94 kivok94 - 2 months ago 6
Java Question

Recursive function doesn't work

I'm writing a program where I give a path of a page and the name class of a tag that mention the next page.

Here's the code that I tried :

public String pagination_get_link(String path, String pagnNextLink_tag) throws IOException{
Document doc = Jsoup.parse(new File(path), "UTF-8");
Element url = doc.getElementById(pagnNextLink_tag);
String url_s = url.attr("href");
pagination_get_link(url_s,pagnNextLink_tag);

return url_s;

}


The example of page is a page like this :
enter link description here

I want to make a recursive method , that loop until get all the links of pagination .
Thanks in advance .

Answer

Your recursive function will keep on running forever, which should make your program run out of stack memory and crash, if it don't crash due to not finding an element that is, but relying on catching exceptions in a case like this do not feel like a good idea.

When making a recursive loop, it has to return the result and NOT call the function itself again if its "done".
That is, if there is no more data to handle, it should be done, now you will just keep on running the loop til it crashes.

I'm not really sure how you want your method to work, but honestly, you don't really seem to need recursion in this case, and I would recommend just using a normal while-loop or similar.


Edit:

If you really want to use your recursive code, returning before calling the function if the url_s is empty should do the trick, something like...

public String pagination_get_link(String path, String pagnNextLink_tag) throws IOException{
    Document doc = Jsoup.parse(new File(path), "UTF-8");
    Element url = doc.getElementById(pagnNextLink_tag);
    String url_s = url.attr("href");
    // Not sure how to check if its empty, so change it to what you need.
    if(url_s == "") { 
      return "";
    }
    pagination_get_link(url_s,pagnNextLink_tag);

    return url_s;
}

Now its possible that it throws an exception when you try to get the element by ID or something similar, in that case, I would rather check if the element exists and if it has a "href" attribute before trying to get it, and if not, just return at that point.

Also, you don't have to return string from the function, as you do nothing with the result. Change it to void and just return; when supposed to return.