输入流阅读器跳过一些文本

发布于 2024-12-07 18:01:24 字数 1049 浏览 2 评论 0原文

大家好,这是我的代码的一个发挥

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

我尝试研究该库并使用调试器,但都没有帮助。

Hi guys so this is an exert from the code I have

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
}

My problem is that I am trying to read in a list of phrases from the URL http://dl.dropbox.com/u/18678304/2011/BSc2/phrases.txt but when it prints out what I have collected it just prints:

aaa

bbb

ddd

I have tried researching the library and using the debugger but neither have helped.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里°也失望 2024-12-14 18:01:24

您应该从 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);
    }

You should remove inputLine = in.readLine(); from inside the while loop, it calls readLine() function a second time, thus skipping every second line.

Your loop should look like this:

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);
    }
徒留西风 2024-12-14 18:01:24

您连续调用 in.readLine() 两次。删除第二个调用。

You are calling in.readLine() twice in a row. Remove the second call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文