Search code examples
javatwitterarraylistprocessingtwitter4j

"unexpected token: int" error on twitter4j program


Im using twitter4j 3.0.3, processing 2, and el capitan. I keep getting errors when trying to run this code! Particularly: unexpected token: int on the line return (int) statuses.stream()

I want that hashtag data to modify the color of the background to a shade of color in between red and green, depending on the number of #good and #bad tweets. I like to think of it as a +100/-100 spectrum. each #good tweet is +1, each #bad is -1. If it is at -100 tweets, then the ellipse is full red. If it is at +100 tweets, then the background is full green.

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

}

The code has problems here:

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

Solution

  • You've got a couple things wrong:

    First, Processing doesn't support Java 8 syntax. This is Java 8 syntax:

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

    You have to refactor this to use a basic for loop, which is in the code I gave you in your other question. Actually you already have a function that uses that, so I don't know why you've added this Java 8 function. Then again, you never declare the tweets variable that you use in that function.

    Secondly, Processing never calls the main() method. You have to get rid of that method and move any of its code to a function that's actually called, like the setup() function.

    Third, you don't have any import statements. I assume you've simply omitted them from your post, but don't make us guess. In the future, try to post an MCVE that shows exactly what you're running instead.