I’m trying to execute my ArrayList
method but it seems that it only grabs the first data.
public class CoronavirusUpdateMain { public static void main(String []args) { Scanner input = new Scanner(System.in); System.out.println(); Scrape sp = new Scrape(); ArrayList<Country>countryData = sp.getCountryData(); System.out.println(countryData); } }
The method I’m trying to execute is countryData
which collects all the data when I run it on a test but when I call the method in main, it only contains 1 value.
final static String url = "https://www.worldometers.info/coronavirus/"; static ArrayList <Country> countryData = new ArrayList<Country>(); public static ArrayList<Country> getCountryData(){ try { ArrayList<Country>countryData = new ArrayList<Country>(); Document doc = Jsoup.connect(url).get(); Elements table = doc.select("#main_table_countries_today"); Elements rows = table.select("tbody").select("tr"); int idx = 0; for (Element row : rows) { Elements eachCountry = row.select("td"); Country curr = new Country(); curr.setId(idx++); curr.setCountry(eachCountry.get(1).text()); curr.setTotalCases(eachCountry.get(2).text()); curr.setNewCases(eachCountry.get(3).text()); curr.setTotalDeaths(eachCountry.get(4).text()); curr.setNewDeaths(eachCountry.get(5).text()); curr.setTotalRecovered(eachCountry.get(6).text()); curr.setActiveCases(eachCountry.get(7).text()); curr.setSeriousCases(eachCountry.get(8).text()); curr.setTotalCasesPerMillionPop(eachCountry.get(9).text()); curr.setTotalDeathsPerMillionPop(eachCountry.get(10).text()); curr.setTotalTests(eachCountry.get(11).text()); curr.setTestsPerMillion(eachCountry.get(12).text()); curr.setPop(eachCountry.get(13).text()); countryData.add(curr); } }catch (IOException e) { e.printStackTrace(); return null; } return countryData; }
This is the result of the main when I call the method
[Countryidx=0, country=’North America’, totalCases=’1,739,671′, newCases=’+3,575′, totalDeaths=’106,293′, newDeaths=’+351′, totalRecovered=’455,831′, activeCases=’1,177,547′, seriousCases=’18,422′, totalCasesPerMillionPop=”, totalDeathsPerMillionPop=”, totalTests=”, testsPerMillion=”, population=”]
This is the result when I test run it in another main
Countryidx=0, country=’North America’, totalCases=’1,739,671′, newCases=’+3,575′, totalDeaths=’106,293′, newDeaths=’+351′, totalRecovered=’455,831′, activeCases=’1,177,547′, seriousCases=’18,422′, totalCasesPerMillionPop=”, totalDeathsPerMillionPop=”, totalTests=”, testsPerMillion=”, population=” Countryidx=1, country=’Europe’, totalCases=’1,825,534′, newCases=’+10,096′, totalDeaths=’164,790′, newDeaths=’+216′, totalRecovered=’805,583′, activeCases=’855,161′, seriousCases=’11,117′, totalCasesPerMillionPop=”, totalDeathsPerMillionPop=”, totalTests=”, testsPerMillion=”, population=” Countryidx=2, country=’South America’, totalCases=’488,006′, newCases=’+349′, totalDeaths=’25,493′, newDeaths=’+15′, totalRecovered=’177,003′, activeCases=’285,510′, seriousCases=’10,599′, totalCasesPerMillionPop=”, totalDeathsPerMillionPop=”, totalTests=”, testsPerMillion=”, population=” Countryidx=3, country=’Asia’, totalCases=’848,074′, newCases=’+6,952′, totalDeaths=’25,601′, newDeaths=’+101′, totalRecovered=’488,138′, activeCases=’334,335′, seriousCases=’4,961′, totalCasesPerMillionPop=”, totalDeathsPerMillionPop=”, totalTests=”, testsPerMillion=”, population=” Countryidx=4, country=’Africa’, totalCases=’93,444′, newCases=’+216′, totalDeaths=’2,926′, newDeaths=’+2′, totalRecovered=’36,971′, activeCases=’53,547′, seriousCases=’287′, totalCasesPerMillionPop=”, totalDeathsPerMillionPop=”, totalTests=”, testsPerMillion=”, population=” Countryidx=5, country=’Oceania’, totalCases=’8,686′, newCases=’+11′, totalDeaths=’121′, newDeaths=”, totalRecovered=’7,990′, activeCases=’575′, seriousCases=’10’, totalCasesPerMillionPop=”, totalDeathsPerMillionPop=”, totalTests=”, testsPerMillion=”, population=” Countryidx=6, country=”, totalCases=’721′, newCases=”, totalDeaths=’15’, newDeaths=”, totalRecovered=’651′, activeCases=’55’, seriousCases=’4′, totalCasesPerMillionPop=”, totalDeathsPerMillionPop=”, totalTests=”, testsPerMillion=”, population=” Countryidx=7, country=’World’, totalCases=’5,004,136′, newCases=’+21,199′, totalDeaths=’325,239′, newDeaths=’+685′, totalRecovered=’1,972,167′, activeCases=’2,706,730′, seriousCases=’45,400′, totalCasesPerMillionPop=’642′, totalDeathsPerMillionPop=’41.7′, totalTests=”, testsPerMillion=”, population=” Countryidx=8, country=’USA’, totalCases=’1,571,018′, newCases=’+435′, totalDeaths=’93,542′, newDeaths=’+9′, totalRecovered=’361,227′, activeCases=’1,116,249′, seriousCases=’17,249′, totalCasesPerMillionPop=’4,749′, totalDeathsPerMillionPop=’283′, totalTests=’12,647,099′, testsPerMillion=’38,234′, population=’330,779,957′ Countryidx=9, country=’Russia’, totalCases=’308,705′, newCases=’+8,764′, totalDeaths=’2,972′, newDeaths=’+135′, totalRecovered=’85,392′, activeCases=’220,341′, seriousCases=’2,300′, totalCasesPerMillionPop=’2,115′, totalDeathsPerMillionPop=’20’, totalTests=’7,500,000′, testsPerMillion=’51,395′, population=’145,927,463′ Countryidx=10, country=’Spain’, totalCases=’278,803′, newCases=”, totalDeaths=’27,778′, newDeaths=”, totalRecovered=’196,958′, activeCases=’54,067′, seriousCases=’1,152′, totalCasesPerMillionPop=’5,963′, totalDeathsPerMillionPop=’594′, totalTests=’3,037,840′, testsPerMillion=’64,977′, population=’46,752,753′ Countryidx=11, country=’Brazil’, totalCases=’271,885′, newCases=”, totalDeaths=’17,983′, newDeaths=”, totalRecovered=’106,794′, activeCases=’147,108′, seriousCases=’8,318′, totalCasesPerMillionPop=’1,280′, totalDeathsPerMillionPop=’85’, totalTests=’735,224′, testsPerMillion=’3,462′, population=’212,385,054′ Countryidx=12, country=’UK’, totalCases=’248,818′, newCases=”, totalDeaths=’35,341′, newDeaths=”, totalRecovered=’N/A’, activeCases=’N/A’, seriousCases=’1,559′, totalCasesPerMillionPop=’3,667′, totalDeathsPerMillionPop=’521′, totalTests=’2,772,552′, testsPerMillion=’40,866′, population=’67,845,213′ Countryidx=13, country=’Italy’, totalCases=’226,699′, newCases=”, totalDeaths=’32,169′, newDeaths=”, totalRecovered=’129,401′, activeCases=’65,129′, seriousCases=’716′, totalCasesPerMillionPop=’3,749′, totalDeathsPerMillionPop=’532′, totalTests=’3,104,524′, testsPerMillion=’51,338′, population=’60,471,682′ Countryidx=14, country=’France’, totalCases=’180,809′, newCases=”, totalDeaths=’28,022′, newDeaths=”, totalRecovered=’62,563′, activeCases=’90,224′, seriousCases=’1,894′, totalCasesPerMillionPop=’2,771′, totalDeathsPerMillionPop=’429′, totalTests=’1,384,633′, testsPerMillion=’21,218′, population=’65,257,220′ Countryidx=15, country=’Germany’, totalCases=’177,842′, newCases=’+15′, totalDeaths=’8,193′, newDeaths=”, totalRecovered=’156,900′, activeCases=’12,749′, seriousCases=’1,115′, totalCasesPerMillionPop=’2,123′, totalDeathsPerMillionPop=’98’, totalTests=’3,147,771′, testsPerMillion=’37,584′, population=’83,753,585′ Countryidx=16, country=’Turkey’, totalCases=’151,615′, newCases=”, totalDeaths=’4,199′, newDeaths=”, totalRecovered=’112,895′, activeCases=’34,521′, seriousCases=’882′, totalCasesPerMillionPop=’1,800′, totalDeathsPerMillionPop=’50’, totalTests=’1,675,517′, testsPerMillion=’19,892′, population=’84,232,553′ Countryidx=17, country=’Iran’, totalCases=’124,603′, newCases=”, totalDeaths=’7,119′, newDeaths=”, totalRecovered=’97,173′, activeCases=’20,311′, seriousCases=’2,698′, totalCasesPerMillionPop=’1,486′, totalDeathsPerMillionPop=’85’, totalTests=’716,176′, testsPerMillion=’8,540′, population=’83,865,579′ Countryidx=18, country=’India’, totalCases=’106,886′, newCases=’+411′, totalDeaths=’3,303′, newDeaths=’+1′, totalRecovered=’42,309′, activeCases=’61,274′, seriousCases=”, totalCasesPerMillionPop=’78’, totalDeathsPerMillionPop=’2′, totalTests=’2,512,388′, testsPerMillion=’1,823′, population=’1,378,418,813′ Countryidx=19, country=’Peru’, totalCases=’99,483′, newCases=”, totalDeaths=’2,914′, newDeaths=”, totalRecovered=’36,524′, activeCases=’60,045′, seriousCases=’883′, totalCasesPerMillionPop=’3,022′, totalDeathsPerMillionPop=’89’, totalTests=’679,582′, testsPerMillion=’20,645′, population=’32,917,154′ Countryidx=20, country=’Canada’, totalCases=’79,112′, newCases=”, totalDeaths=’5,912′, newDeaths=”, totalRecovered=’40,050′, activeCases=’33,150′, seriousCases=’502′, totalCasesPerMillionPop=’2,098′, totalDeathsPerMillionPop=’157′, totalTests=’1,339,971′, testsPerMillion=’35,540′, population=’37,703,672′ Countryidx=21, country=’Saudi Arabia’, totalCases=’59,854′, newCases=”, totalDeaths=’329′, newDeaths=”, totalRecovered=’31,634′, activeCases=’27,891′, seriousCases=’251′, totalCasesPerMillionPop=’1,722′, totalDeathsPerMillionPop=’9′, totalTests=’618,084′, testsPerMillion=’17,787′, population=’34,748,812′ Countryidx=22, country=’Belgium’, totalCases=’55,983′, newCases=’+192′, totalDeaths=’9,150′, newDeaths=’+42′, totalRecovered=’14,847′, activeCases=’31,986′, seriousCases=’313′, totalCasesPerMillionPop=’4,833′, totalDeathsPerMillionPop=’790′, totalTests=’720,228′, testsPerMillion=’62,175′, population=’11,583,877′ Countryidx=23, country=’Mexico’, totalCases=’54,346′, newCases=’+2,713′, totalDeaths=’5,666′, newDeaths=’+334′, totalRecovered=’37,325′, activeCases=’11,355′, seriousCases=’378′, totalCasesPerMillionPop=’422′, totalDeathsPerMillionPop=’44’, totalTests=’185,755′, testsPerMillion=’1,442′, population=’128,773,952′ Countryidx=24, country=’Chile’, totalCases=’49,579′, newCases=”, totalDeaths=’509′, newDeaths=”, totalRecovered=’21,507′, activeCases=’27,563′, seriousCases=’876′, totalCasesPerMillionPop=’2,596′, totalDeathsPerMillionPop=’27’, totalTests=’397,200′, testsPerMillion=’20,799′, population=’19,097,135′ Countryidx=25, country=’Pakistan’, totalCases=’45,898′, newCases=’+1,932′, totalDeaths=’985′, newDeaths=’+46′, totalRecovered=’13,101′, activeCases=’31,812′, seriousCases=’111′, totalCasesPerMillionPop=’208′, totalDeathsPerMillionPop=’4′, totalTests=’414,254′, testsPerMillion=’1,880′, population=’220,368,526′ Countryidx=26, country=’Netherlands’, totalCases=’44,249′, newCases=”, totalDeaths=’5,715′, newDeaths=”, totalRecovered=’N/A’, activeCases=’N/A’, seriousCases=’293′, totalCasesPerMillionPop=’2,583′, totalDeathsPerMillionPop=’334′, totalTests=’302,395′, testsPerMillion=’17,652′, population=’17,130,596′ Countryidx=27, country=’Qatar’, totalCases=’35,606′, newCases=”, totalDeaths=’15’, newDeaths=”, totalRecovered=’5,634′, activeCases=’29,957′, seriousCases=’172′, totalCasesPerMillionPop=’12,384′, totalDeathsPerMillionPop=’5′, totalTests=’166,182′, testsPerMillion=’57,799′, population=’2,875,180′ Countryidx=28, country=’Ecuador’, totalCases=’34,151′, newCases=”, totalDeaths=’2,839′, newDeaths=”, totalRecovered=’3,457′, activeCases=’27,855′, seriousCases=’221′, totalCasesPerMillionPop=’1,939′, totalDeathsPerMillionPop=’161′, totalTests=’98,171′, testsPerMillion=’5,574′, population=’17,610,964′ Countryidx=29, country=’Belarus’, totalCases=’31,508′, newCases=”, totalDeaths=’175′, newDeaths=”, totalRecovered=’10,620′, activeCases=’20,713′, seriousCases=’92’, totalCasesPerMillionPop=’3,334′, totalDeathsPerMillionPop=’19’, totalTests=’375,076′, testsPerMillion=’39,692′, population=’9,449,669′ Countryidx=30, country=’Sweden’, totalCases=’30,799′, newCases=”, totalDeaths=’3,743′, newDeaths=”, totalRecovered=’4,971′, activeCases=’22,085′, seriousCases=’287′, totalCasesPerMillionPop=’3,052′, totalDeathsPerMillionPop=’371′, totalTests=’209,900′, testsPerMillion=’20,799′, population=’10,092,027′ Countryidx=31, country=’Switzerland’, totalCases=’30,618′, newCases=”, totalDeaths=’1,891′, newDeaths=”, totalRecovered=’27,700′, activeCases=’1,027′, seriousCases=’57’, totalCasesPerMillionPop=’3,541′, totalDeathsPerMillionPop=’219′, totalTests=’350,099′, testsPerMillion=’40,486′, population=’8,647,311′ Countryidx=32, country=’Portugal’, totalCases=’29,432′, newCases=”, totalDeaths=’1,247′, newDeaths=”, totalRecovered=’6,431′, activeCases=’21,754′, seriousCases=’101′, totalCasesPerMillionPop=’2,885′, totalDeathsPerMillionPop=’122′, totalTests=’662,375′, testsPerMillion=’64,939′, population=’10,199,983′
This is the updated code in the method. Static has to be removed as it doesn’t change the value of the ArrayList
, I also had to remove the 2 unnecessary return as it doesn’t return the complete ArrayList
.
public ArrayList<Country> getCountryData(){ ArrayList<Country>countryData = new ArrayList<Country>(); try { Document doc = Jsoup.connect(url).get(); Elements table = doc.select("#main_table_countries_today"); Elements rows = table.select("tbody").select("tr"); int idx = 0; for (Element row : rows) { Elements eachCountry = row.select("td"); Country curr = new Country(); curr.setId(idx++); curr.setCountry(eachCountry.get(1).text()); curr.setTotalCases(eachCountry.get(2).text()); curr.setNewCases(eachCountry.get(3).text()); curr.setTotalDeaths(eachCountry.get(4).text()); curr.setNewDeaths(eachCountry.get(5).text()); curr.setTotalRecovered(eachCountry.get(6).text()); curr.setActiveCases(eachCountry.get(7).text()); curr.setSeriousCases(eachCountry.get(8).text()); curr.setTotalCasesPerMillionPop(eachCountry.get(9).text()); curr.setTotalDeathsPerMillionPop(eachCountry.get(10).text()); curr.setTotalTests(eachCountry.get(11).text()); curr.setTestsPerMillion(eachCountry.get(12).text()); curr.setPop(eachCountry.get(13).text()); countryData.add(curr); } }catch (IOException e) { e.printStackTrace(); } return countryData; }
Advertisement
Answer
Make the changes as follows:
//static ArrayList <Country> countryData = new ArrayList<Country>();<--- Remove this public static ArrayList<Country> getCountryData(){ ArrayList<Country>countryData = new ArrayList<Country>(); //<--- Move it from within the try...catch block to here try { Document doc = Jsoup.connect(url).get(); //... } catch (IOException e) { e.printStackTrace(); // return null; <--- Remove this } return countryData; }
Explanation (thanks to Thomas):
The local variable countryData
is hiding the static
variable of the same name but only inside the try-block
, thus the original return countryData()
actually returns the static
list which is empty with the given code.