I am trying to find unique hashtags from a tweet that a user inputs. I have the code to find the number of times a word is used in the input, but I just need to know the number of different hashtags used. For example, in the input

#one #two blue red #one #green four

there would be 3 unique hashtags as #one, #two, and #green. I cannot figure out how to code this.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Tweet {

public static void main(String[] args) {
    Scanner hashtag = new Scanner( System.in );
    System.out.println( "Please enter a line of text" );
    String userInput = hashtag.nextLine();

    userInput = userInput.toLowerCase();

    userInput = userInput.replaceAll( "\\W", " " );     // strip out any non words.
    userInput = userInput.replaceAll( "  ", " " );      // strip out any double spaces
                                                        //   created from stripping out non words
                                                        //   in the first place!
    String[] tokens = userInput.split( " " );
    System.out.println( userInput );

    ArrayList< String > tweet = new ArrayList< String >();

    tweet.addAll( Arrays.asList( tokens ) );

    int count = 0;

    for( int i = 0; i < tweet.size(); i++ )
        System.out.printf( "%s: ", tweet.get( i ) );
        for( int j = 0; j < tweet.size(); j++ )
            if( tweet.get( i ).equals( tweet.get( j ) ) )
            if( tweet.get( i ).equals( tweet.get( j ) ) && count > 1 )
                tweet.remove( j );                      // after having counted at least
        }                                               // one, remove duplicates from List        
        System.out.printf( "%d\n", count );
        count = 0;

closed as off-topic by Jamal, Brian Reichle, palacsint, Nikita B, JohnMark13 Oct 7 '13 at 9:43

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for code to be written to solve a specific problem are off-topic here as there is no code to review." – Jamal, Brian Reichle, palacsint, JohnMark13
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ You might consider using a Set<String> to hold the hashtags. \$\endgroup\$ – icktoofay Oct 7 '13 at 0:31

If it is not program for one purpose and then you will throw it away i will not put to much code in main method. I would just initialize the application, inputs and outputs in main and than call start, run or some other method.

Also represent tweet as an object with methods Tweet#parse Tweet#getHashTags and so on. Or you can Create TweetParser and separate it even more, but i think in this case it is overkill.

For tokenizing the string use built-in class StringTokenizer And use Set as suggested in comment.

My Tweet class:

package scjp.tweet;

import java.util.*;

public class Tweet {

    private final List<String> hashtags = new ArrayList<String>();

    private String text;

    private Tweet() {}

    public static Tweet getTweet(String tweetText) {
        Tweet tweet = new Tweet();
        tweet.text = tweetText;
        return tweet;

    private void parse() {
        StringTokenizer tokenizer = new StringTokenizer(this.text);
        while (tokenizer.hasMoreTokens()) {
            String token = tokenizer.nextToken();
            if (token.startsWith("#")) {

    public Set<String> getUniqueHashtags() {
        Set<String> unique = new HashSet<String>();
        return unique;


And main class:

package scjp.tweet;

import java.util.Scanner;

public class TweetMain {

    public static void main(String... args) {
        Scanner hashtag = new Scanner( System.in );
        System.out.println( "Please enter a line of text" );
        String tweetText = hashtag.nextLine();

        Tweet tweet = Tweet.getTweet(tweetText.toLowerCase());


Not the answer you're looking for? Browse other questions tagged or ask your own question.