1

我正在构建一个 Twitter 客户端,我不想检索和显示全球趋势。到目前为止(在某种程度上感谢 Stack Overflow 的帮助)我可以检索趋势信息,从中提取必要的信息并将趋势发布到控制台。当我尝试将趋势添加到表格中时,我只能多次显示第一个趋势,而且我不确定我的行创建等哪里出了问题。

一双新鲜的眼睛将不胜感激!

谢谢

public static void WorldWideTrends() {
    Trends WorldWideTrendsList;

    try {

        WorldWideTrendsList = getTrends();
        UI.whatIsDisplayedList.removeAll();
        UI.tweetModel = new DefaultTableModel(10, 1);
        String trendsInfo = WorldWideTrendsList.toString();

        System.out.println(trendsInfo);

        Pattern p = Pattern.compile("(#.*?)\\'", Pattern.DOTALL);
        Matcher matcher = p.matcher(trendsInfo);

        while (matcher.find()) {

            for (int i = 0; i < 10; i++) {
                String output = matcher.group(0);

                System.out.println(output);
                UI.tweetModel.insertRow(1, new Object[] {});
                UI.tweetModel.setValueAt(
                        "<html><body style='width: 400px;'><strong>"
                                + output + "</strong><html><br>", i, 0);
            }
        }

    } catch (TwitterException e) {
        e.printStackTrace();
    }

    UI.whatIsDisplayedList.setModel(UI.tweetModel);

}
4

1 回答 1

0

我不确定这样做的目的是什么:

while (matcher.find()) {
    for (int i = 0; i < 10; i++) {
       String output = matcher.group(0);
       ...
    }
}

但它会处理每场比赛10次。只是.group()再次呼叫不会带您进入下一场比赛,您需要.find()再次呼叫。

我认为您想简单地删除 for 循环(但如果存在超过 10 个匹配项,这将匹配超过 10 次),或者可能删除 while 循环并执行以下操作:

// process the first 10 matches
// no while-loop!
for (int i = 0; i < 10 && matcher.find(); i++) {
   String output = matcher.group(0);
   ...
}
于 2013-03-22T07:42:13.877 回答