1

Okay so I'm having a problem with scope of an object. I'm using Jsoup right now and here's the code:

//Website is /001.shtml, so adding count to the string wouldn't work. //This is why I have the 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 so I create the Document object called site, and I need to add a certain amount of zeros because of how the person made the website, no problem. However when I try to use site.select(anything), I can't because the object was defined in the if construct.

Also, I can not initialize it outside the if, it does not work because I get a duplicate error thrown. Please tell me there is a fix for this because I have searched and searched to no avail, and I don't want to have to put the rest of the program 3 times into different ifs...

2
  • 1
    You should be using String formatter. like this would make the input 3 digits: String.format("%03d", numberValueHere) Commented Nov 3, 2014 at 4:32
  • What do you mean by "it does not work?" Please provide an error message and include a minimal program that triggers the error. Commented Nov 3, 2014 at 5:10

2 Answers 2

1

Move the declaration outside the if else if chain, like

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

Or you might build the url and then connect once like,

String urlStr = url + String.format("%03d", count) + ".shtml"; Document site = Jsoup.connect(urlStr).get(); 
Sign up to request clarification or add additional context in comments.

1 Comment

I have already tried that, unfortunately it does not work and I can't figure out why.
1

Just declare site outside the if..else blocks:

Document site; if (count < 10){ site = Jsoup.connect(url + "00" + count).get(); } else if (count < 100) { site = Jsoup.connect(url + "0" + count + ".shtml").get(); } else { site = Jsoup.connect(url + count + ".shtml").get(); } 

Alternatively, you can use nested ternary operators:

Document site = Jsoup.connect( count < 10 ? url + "00" + count : count < 100 ? url + "0" + count + ".shtml" : url + count + ".shtml" ).get(); 

If I'm correct that your code has a bug and the count < 10 case is missing + ".shtml", then the best solution is:

Document site = Jsoup.connect(url + String.format("%03d.shtml", count)).get(); 

3 Comments

I have already tried that and it does not work due to duplicate of site.
@RobertStanton - What duplicate of site? Note that in my code I've eliminated the type name when site is used inside the if blocks.
+1 for not assigning null in the declaration, so the compiler will warn you if you forget to properly initialize it before using it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.