【发布时间】:2016-06-25 22:25:10
【问题描述】:
我正在使用 twitter4j 3.0.3、处理 2 和 el capitan。尝试运行此代码时,我不断收到错误消息!特别是: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();
}
【问题讨论】:
-
你是如何编辑这段代码的?您使用的是 Processing 编辑器,还是使用 Eclipse 之类的 IDE?我也没有看到任何导入语句。请张贴minimal reproducible example,显示您实际运行的内容。
-
我正在使用处理编辑器进行编辑。我对问题添加了一个编辑,希望它更清楚。
标签: java twitter arraylist processing twitter4j