gpt4 book ai didi

java - twitter4j 程序上出现 "unexpected token: int"错误

转载 作者:行者123 更新时间:2023-12-01 10:15:29 25 4
gpt4 key购买 nike

我正在使用 twitter4j 3.0.3、processing 2 和 el Captain。尝试运行此代码时我不断收到错误!特别是:unexpected token: int上线return (int) statuses.stream()

我希望主题标签数据将背景颜色修改为红色和绿色之间的颜色,具体取决于 #good 和 #bad 推文的数量。我喜欢将其视为 +100/-100 频谱。每条#good 推文为+1,每条#bad 推文为-1。如果是 -100 条推文,则椭圆为全红色。如果超过 100 条推文,则背景为全绿色。

ConfigurationBuilder cb = new ConfigurationBuilder();
Twitter twitterInstance;
Query queryForTwitter;

//ArrayList tweets;

void setup() {
cb.setOAuthConsumerKey("xxxx");
cb.setOAuthConsumerSecret("xxxx");
cb.setOAuthAccessToken("xxxx");
cb.setOAuthAccessTokenSecret("xxxx");
cb.setUseSSL(true);

size(640,440);

} //setup

//ArrayList<String> tweets = new ArrayList<String>();

public static void main (String args[]) throws TwitterException {
Twitter twitter = new TwitterFactory().getInstance();
List<Status> statuses = twitter.getUserTimeline("google");
String hashtag = "#AlphaGo";
System.out.println("The Twitter page contains "
+ countTweets(hashtag, statuses)
+ " tweets with the hashtag : " + hashtag);

}

public static int countTweets(String hashtag, List<Status> statuses){
return (int) statuses.stream()
.filter(x -> x.getText().contains(hashtag))
.count();
}

//create a function that counts the tweets
//that contain a certain hashtag
int countTweets(String hashtag){
int total = 0;
for(String tweet : tweets){
if(tweet.contains(hashtag)){
total++;
}
}
return total;
}

void draw(){

//count the good and bad tweets
int goodTweets = countTweets("#good");
int badTweets = countTweets("#bad");

//calculate color based on tweet counts
float r = badTweets/100.0 * 255;
float g = goodTweets/100.0 * 255;
float b = 0;

background(r, g, b);

}

这里的代码有问题:

public static void main (String args[]) throws TwitterException {
Twitter twitter = new TwitterFactory().getInstance();
List<Status> statuses = twitter.getUserTimeline("google");
String hashtag = "#AlphaGo";
System.out.println("The Twitter page contains "
+ countTweets(hashtag, statuses)
+ " tweets with the hashtag : " + hashtag);

}

public static int countTweets(String hashtag, List<Status> statuses){
return (int) statuses.stream()
.filter(x -> x.getText().contains(hashtag))
.count();
}

最佳答案

你有几处错误:

首先,处理不支持 Java 8 语法。这是 Java 8 语法:

statuses.stream().filter(x -> x.getText().contains(hashtag)).count();

您必须重构它以使用基本的 for 循环,该循环位于我在其他问题中给您的代码中。实际上你已经有一个使用它的函数,所以我不知道你为什么添加这个 Java 8 函数。话又说回来,您永远不会声明在该函数中使用的 tweets 变量。

其次,处理永远不会调用 main() 方法。您必须摆脱该方法并将其任何代码移动到实际调用的函数中,例如setup() 函数。

第三,您没有任何导入语句。我假设您只是在帖子中省略了它们,但请不要让我们猜测。以后,尝试发布MCVE这准确地显示了您正在运行的内容。

关于java - twitter4j 程序上出现 "unexpected token: int"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35927275/

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