gpt4 book ai didi

java - 使用 Regex 和 DefaultTableModel 将值插入到 JTable 中

转载 作者:行者123 更新时间:2023-12-01 04:47:42 25 4
gpt4 key购买 nike

我正在构建一个 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);

}

最佳答案

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

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);
...
}

关于java - 使用 Regex 和 DefaultTableModel 将值插入到 JTable 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15560967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com