-1

I'm going to do a sentimental analysis using twitter hashtag and I'm using Java for this project. How do I code Java to search for a specific hashtag and show the text?

This is what I've tried:

public static void main(String[] args) throws TwitterException {
    ConfigurationBuilder configurationBuider = new ConfigurationBuilder();
    configurationBuider.setDebugEnabled(true)
            .setOAuthConsumerKey("")
            .setOAuthConsumerSecret("")
            .setOAuthAccessToken("")
            .setOAuthAccessTokenSecret("");

    TwitterFactory tf=new TwitterFactory(configurationBuider.build());
    twitter4j.Twitter twitter=tf.getInstance();

    List<Status> status=twitter.getHomeTimeline();
    for(Status s:status){
        System.out.println(s.getUser().getName()+"    "+s.getText());
    }
}

}

What I get is only username and the text on timeline. Thank you for your help guys.

Clonkex
  • 3,373
  • 7
  • 38
  • 55

2 Answers2

0

The question isn't entirely clear on what you are wanting to do but here is my interpretation:

You want to search the hashtags of the 20 most recent twitter posts on the timeline for a particular hashtag. If that post contains the hashtag, you want to display the user who posted it and the text of the post.

Below is what I believe to be a working example of this.

public static void main(String[] args) throws TwitterException {
ConfigurationBuilder configurationBuider = new ConfigurationBuilder();
configurationBuider.setDebugEnabled(true)
        .setOAuthConsumerKey("")
        .setOAuthConsumerSecret("")
        .setOAuthAccessToken("")
        .setOAuthAccessTokenSecret("");

TwitterFactory tf=new TwitterFactory(configurationBuider.build());
twitter4j.Twitter twitter=tf.getInstance();

// You can pull this in from user input, etc.
String hashTagToSearchFor = "testme";

List<Status> status=twitter.getHomeTimeline();
for(Status s:status){
    HashtagEntity[] hashTags = s.getHashtagEntities();
    for (HashtagEntity hashtag : hashTags) {
        if (hashtag.equals(hashTagToSearchFor)) {
            System.out.println(s.getUser().getName() + ": " + s.getText());

            // Assuming all hashtags are unique...
            continue;
        }
    }
}
}

Please let me know if I have made any incorrect assumptions and I will be glad to edit my answer to make adjustments.

Kylon Tyner
  • 371
  • 2
  • 12
  • Thanks man. I've tried your code and it showed up nothing. The code run successfully and I put "sport" in the Hashtag to search for but there is no comment that use #sport show up. – D POSITIVE Daisuke Jun 14 '17 at 09:52
  • do you have any social app that we can talk easier ? – D POSITIVE Daisuke Jun 14 '17 at 10:42
  • I do not unfortunately. The first thing I would do is to print out the whole list of hashtagentries for each post and see what they are because remember: twitter.getHomeTimeline() only returns 20 posts. You can explore the twitter4j javadoc to see what other methods are available to get posts. – Kylon Tyner Jun 14 '17 at 11:30
  • how can I print out the whole list ? – D POSITIVE Daisuke Jun 14 '17 at 14:05
  • Take out the if statement that is checking for hashtag.equals(hashTagToSearchFor) and instead just use System.out.println(hashtag) or possibly hashtag.toString() I don't know if it's a String or not off the top of my head – Kylon Tyner Jun 14 '17 at 14:40
  • Check out this page if you get lost: http://twitter4j.org/oldjavadocs/4.0.0/index.html – Kylon Tyner Jun 14 '17 at 17:18
0

Thank you man I already solved by doing this Kylon Tyner

    Query query = new Query("flood");
    QueryResult result = twitter.search(query);
    for (Status status : result.getTweets()) {
        System.out.println("@" + status.getUser().getScreenName() + " : " + status.getText() + " : " + status.getGeoLocation());
    }