Sunday, 15 February 2015

Scope of Variables in Java -- issue -



Scope of Variables in Java -- issue -

okay i'm having problem scope of object. i'm using jsoup right , here's code:

//website /001.shtml, adding count string wouldn't work. //this why have ifs if (count < 10) { document site = jsoup.connect(url + "00" + count).get(); } else if (count < 100) { document site = jsoup.connect(url + "0" + count + ".shtml").get(); } else { document site = jsoup.connect(url + count + ".shtml").get(); }

okay create document object called site, , need add together amount of zeros because of how person made website, no problem. when seek utilize site.select(anything), can't because object defined in if construct.

also, can not initialize outside if, not work because duplicate error thrown. please tell me there prepare because have searched , searched no avail, , don't want have set rest of programme 3 times different ifs...

move declaration outside if else if chain, like

document site = null; if (count < 10) { site = jsoup.connect(url + "00" + count + ".shtml").get(); // missing shtml. } else if (count < 100) { site = jsoup.connect(url + "0" + count + ".shtml").get(); } else { site = jsoup.connect(url + count + ".shtml").get(); }

or might build url , connect 1 time like,

string urlstr = url + string.format("%03d", count) + ".shtml"; document site = jsoup.connect(urlstr).get();

java

No comments:

Post a Comment