java - Hashtag Multiple -
i trying find number of unique hashtags in tweet inputted user. example, if inputs "#one #seven #one purple green #red", show 3 unique hashtags "#one, #seven, #red". in code, can 1 tweet, cannot figure out how input multiple tweets , find unique hashtags them.
package edu.bsu.cs121.jmgibson; import java.util.arraylist; import java.util.arrays; import java.util.hashset; import java.util.scanner; import java.util.set; public class tweet { public static void main(string[] args) { scanner scanner = new scanner(system.in); system.out.println("please enter tweet"); string tweet = scanner.nextline(); set<string> hashtags = gethashtags(tweet); system.out.println(hashtags.tostring()); } public static set<string> gethashtags(string tweet) { string[] words = tweet.split(" "); set<string> hashtags = new hashset<string>(); (string word : words) { if (word.startswith("#")) { hashtags.add(word); } } return hashtags; } }
i don't want homework you, i'll offer suggestions:
in
main(), you'll need loop asks input , makes callsgethashtags().instead of creating new
hashsetinside ofgethashtags(), create 1 inmain()(outside of loop), , pass in.
Comments
Post a Comment