Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ArrayList方法逻辑问题_Java_Jsoup - Fatal编程技术网

Java ArrayList方法逻辑问题

Java ArrayList方法逻辑问题,java,jsoup,Java,Jsoup,我试图执行我的ArrayList方法,但它似乎只获取第一个数据 公共类CoronavirusUpdateMain{ 公共静态void main(字符串[]args){ 扫描仪输入=新扫描仪(System.in); System.out.println(); 刮伤sp=新刮伤(); ArrayListcountryData=sp.getCountryData(); System.out.println(countryData); } } 我试图执行的方法是countryData,当我在测试中运行

我试图执行我的
ArrayList
方法,但它似乎只获取第一个数据

公共类CoronavirusUpdateMain{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println();
刮伤sp=新刮伤();
ArrayListcountryData=sp.getCountryData();
System.out.println(countryData);
}
}
我试图执行的方法是
countryData
,当我在测试中运行它时,它收集所有数据,但当我在main中调用该方法时,它只包含1个值

最终静态字符串url=”https://www.worldometers.info/coronavirus/";
静态ArrayList countryData=新ArrayList();
公共静态ArrayList getCountryData(){
试一试{
ArrayListcountryData=新的ArrayList();
Document doc=Jsoup.connect(url.get();
Elements table=doc.select(“#main_table_countries_today”);
元素行=表格。选择(“tbody”)。选择(“tr”);
int-idx=0;
用于(元素行:行){
元素每个国家=行。选择(“td”);
国家/地区当前=新国家/地区();
当前设置ID(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(每个country.get(8.text());
curr.setTotalCasesPerMillionPop(eachCountry.get(9.text());
curr.setTotalDeathPerMillionPop(eachCountry.get(10.text());
curr.setTotalTests(eachCountry.get(11.text());
curr.setTestsPerMillion(eachCountry.get(12.text());
curr.setPop(eachCountry.get(13.text());
countryData.add(货币);
}
}捕获(IOE异常){
e、 printStackTrace();
返回null;
}
返回国家数据;
}
这是调用该方法时main的结果

[Countryidx=0,country='北美',totalCases='1739671',newCases='3575',totalDeaths='106293',newDeaths='351',totalRecovered='455831',activeCases='1177547',seriousCases='18422',totalCasesPerMillionPop=''',totalTests='106293',newDeaths='351',totalRecovered='455831',activeCases='1177547',seriousCases='18422',totalCases


这是我在另一个main中测试运行它时的结果

Countryidx=0,country='北美',totalCases='1739671',newCases='3575',totalDeaths='106293',newDeaths='351',totalRecovered='455831',activeCases='1177547',seriousCases='18422',totalCasesPerMillionPop=''',totalTests='106293',newDeaths='455831',activeCases='1177547',totalCasesPerMillionPop='18422',TotalCaseSPermillionPo Countryidx=1,country='Europe',totalCases='1825534',newCases='10096',totalDeaths='164790',newDeaths='216',totalRecovered='805583',activeCases='855161',seriousCases='11117',totalCasesPerMillionPop='',totalTests='164790',newDeaths='805583',activeCases='855161',totalCasesPerMillionPop='11117',TotalDeathPermillion Countryidx=2,country='南美洲',totalCases='488006',newCases='349',TotalDeathers='25493',NewDeathers='15',totalRecovered='177003',activeCases='285510',seriousCases='10599',totalCasesPerMillionPop='',TotalTestsPermillionPop='',testsPerMillion='',population='' Countryidx=3,country='Asia',totalCases='848074',newCases='6952',totalDeaths='25601',newDeaths='101',totalRecovered='488138',activeCases='334335',seriousCases='4961',totalCasesPerMillionPop='',totalTests='25601',newDeaths='488138',activeCases='334335',totalCasesPerMillionPop='4961',TotalCaseSPermillionPo Countryidx=4,country='非洲',totalCases='93444',newCases='216',totalDeaths='2926',newDeaths='2',totalRecovered='36971',activeCases='53547',seriousCases='287',totalCasesPerMillionPop=''',totalTests='2926',newDeaths='2',totalRecovered='36971',activeCases='53547',totalCasesPerMillionPop='287',TotalCas Countryidx=5,country='Oceania',totalCases='8686',newCases='11',TotalDeatures='121',NewDeatures=''',totalRecovered='7990',activeCases='575',seriousCases='10',totalCasesPerMillionPop='10',TotalDeathPermillionPop='12',totalTests='121',TestPermillion='7990',population='' Countryidx=6,country='',totalCases='',newCases='',totalDeaths='',newDeaths='',totalDeaths='',totalDeaths='',totalDeaths='',totalDeaths='',newDeaths='',totalDeaths='',totalDeaths='',totalDeaths='',totalDeaths='',totalDeaths='',totalDeaths='',totalDeaths='' Countryidx=7,country='World',totalCases='5004136',newCases='21199',TotalDeathers='325239',NewDeathers='685',totalRecovered='1972167',activeCases='2706730',Seriouses='45400',totalCasesPerMillionPop='642',TotalDeathPermillionPop='41.7',totalTests='',TestPermillion='41.7',人口='' Countryidx=8,country='USA',totalCases='1571018',newCases='435',totalDeaths='93542',newDeaths='9',totalRecovered='361227',activeCases='1116249',seriousCases='17249',totalCasesPerMillionPop='4749',TotalDeathPermillionPop='283',totalTests='12647099',TestPermillion='38234',人口='330779957' Countryidx=9,country='Russia',totalCases='308705',New Cases='8764',totalDeaths='2972',newDeaths='135',totalRecovered='85392',Active Cases='220341',seriousCases='2300',totalCasesPerMillionPop='2115',totalTests='7500000',TestPermillion='51395',Pop='145927463' Countryidx=10,country='西班牙',totalCases='278803',newCases='27778',newDeaths='''',totalRecovered='196958',activeCases='54067',seriousCases='1152',totalCasesPerMillionPop='5963',TotalDeathPermillionPop='5963',TotalDeathPermillionPop'=
//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;
}