嗨,大家好,这是我编写的代码所发挥的作用
public ItemList() throws Exception {
//itemList = new List<Item>() ;
List<Item> itemList = new ArrayList<Item>() ;
URL itemPhrases = new URL("http://dl.dropbox.com/u/18678304/2011/BSc2/phrases.txt"); // Initilize URL
BufferedReader in = new BufferedReader(new InputStreamReader(
itemPhrases.openStream())); // opens Stream from html
while ((inputLine = in.readLine()) != null) {
inputLine = in.readLine();
System.out.println(inputLine);
Item x = new Item(inputLine);
itemList.add(x);
} // validates and reads in list of phrases
for(Item item: itemList){
System.out.println(item.getItem());
}
in.close();// ends input stream
}
我的问题是我试图从URL http://dl.dropbox.com/u/18678304/2011/BSc2/phrases.txt中读取短语列表,但是当它打印出我收集的内容时,它只会打印:
aaa
bbb
ddd
我曾尝试研究该库并使用调试器,但都没有帮助。
最佳答案
您应该从while循环内部删除inputLine = in.readLine();
,它再次调用readLine()
函数,从而跳过第二行。
您的循环应如下所示:
while ((inputLine = in.readLine()) != null) {
//this line must not be here inputLine = in.readLine();
System.out.println(inputLine);
Item x = new Item(inputLine);
itemList.add(x);
}