我试图从用户输入的推文中找到唯一的主题标签。我的兄弟正在帮助我,但他不得不离开。无论如何,所以我有代码来查找输入中使用单词的次数,但我只需要知道使用的不同主题标签的数量。例如,在输入“#one #two blue red #one #greenfour”中,将有 3 个唯一的主题标签,即 #one、#two 和 #green。我无法弄清楚如何编码。
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 ) ) )
count++;
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;
}
} }