2014-04-20 60 views
0

我想从45页中读取完全相同的内容(除了读取当然的部分)并将它们写入行列表的列表中。Java - BufferedReader不从URL中读取

我写了这个代码至今:

public static ArrayList<ArrayList<String>> linesWeNeed(){ 
    ArrayList<ArrayList<String>> returnListList = new ArrayList<ArrayList<String>>(); 
    for(int i = 1; i<=45; i++){ 
     int pageNum=(i*20)-20; 
     System.out.println("PageNum"+pageNum); 
     URL url=null; 
     try { 
      url = new URL("http://tq.mot.gov.il/index.php?option=com_moofaq&iotype=w&view=category&lang_ovrrde=ENG&id=3&Itemid=29&limitstart="+pageNum); 
     } catch (MalformedURLException e) { 
      System.out.println("Oh uh!"); 
     } 

     BufferedReader in = null; 
     try { 
      in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8")); 
     } catch (IOException e) { 
      System.out.println("FATAL ERROR"); 

      e.printStackTrace(); 
     } 
     ArrayList<String> lineCodes = new ArrayList<String>(); 
     ArrayList<String> linesWeNeed = new ArrayList<String>(); 
     String line; 
     try { 
      readLines: 
      while((line=in.readLine())!=null){ 
       if(line.contains("</tr></table></div></div></li></ul></div></div><br /></div>")){ 
        break readLines; 
       } 
       lineCodes.add(line); 
      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     for(int o = 1; o>=lineCodes.size(); o++){ 
      String readLine = lineCodes.get(o-1); 
      if(o>=297){ 
       linesWeNeed.add(readLine); 
      } 

     } 
     returnListList.add(linesWeNeed); 
    } 
    return returnListList; 
} 

有没有错误,但由于某些原因ArrayList中每个列表的方法的返回是空的。为什么?

提前致谢!

回答

3
for(int o = 1; o>=lineCodes.size(); o++){ 

您的循环条件回到前面。尝试< =。

+0

谢谢,有时我可以真的很愚蠢。 :P – NonameSL

1

对(INT O = 1;○> = lineCodes.size();○++){

仔细检查循环条件存在;看起来像块是永远不会执行...

相关问题