Search code examples
javaprocessingtwitter4j

Twitter4j counting the number of tweets found


Here's my method for finding a hashtag:

void getNewTweets()
{
  //try the search
  try
  {
    Query query = new Query(searchString);
    //get the last 50 tweets
    query.count(2);
    QueryResult result = twitter.search(query);
    tweets = result.getTweets();

    System.out.println(tweets); 
  }  
  //if there is an error then catch it and print it out
  catch (TwitterException te)
  {
    System.out.println("Failed to search tweets: " + te.getMessage());
    System.exit(-1);
  }
}

I'm using the Twitter4j library. How would I count the number of tweets found?


Solution

  • Store the tweets in an ArrayList and fetch the size of the ArrayList to display the number of tweets.

    ArrayList tweets = (ArrayList) result.getTweets();
    System.out.println(tweets.size());