问题
I have few questions about my assignment.
The assignment is to let user enter a sentence, and the program counts each word's frequency, when user enters an empty string, quit the program. Also, the program is case sensitive. For example, Apple is an apple is a phone, the result is that Apple-1; is-2; an-1; a-1; phone-1. Here is my code:
public static void main(String[] args)
{
while (true)
{
System.out.println("Enter a sentence:");
Scanner keyboard = new Scanner(System.in);
String sentence = keyboard.nextLine();
if (sentence.isEmpty()) // quit the program when user enter an empty string
{
break;
}
else
{
StringTokenizer st = new StringTokenizer(sentence);
while (st.hasMoreTokens())
{
List<String> sentenceElement = new ArrayList<String>();
sentenceElement.add(st.nextToken());
}
System.out.println(sentenceElement);
}
}
I have few questions.
- I try to save all tokens into an array called sentenceElement, and try to output, but failed. The compiler shows
error: cannot find symbol System.out.println(sentenceElement);
- How can I count the frequency of each word?
Thank you so much, and I really appreciate your answers and solutions.
回答1:
You can convert the Input in to tokens by using
String tokens[]=input.split(" ");
Now next is to count frequency of each Word. You can use Hashmap for that.
HashMap < String, Integer > hmap = new HashMap < Integer, String > ();
for (str: tokens) {
if (hmap.get(str) == null) hmap.put(str, 1);
else hmap.put(str, hmap.get(str) + 1);
}
Iterator it = hmap.iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
System.out.println(pair.getKey() + " = " + pait.getValue());
it.remove();
}
回答2:
- How can I count the frequency of each word?
Use HashMap to store the word as a key and the count as value. Then loop over all the words and first get the word as key from hashmap if it returns null then add the letter to hashmap with value 1 if same key comes in loop then get of hashmap will not return null it will return the old count which is 1 not increment it to 2 and again store it back at last when all the words are done you have the count in your hashmap just iterate it and print the key->value .
来源:https://stackoverflow.com/questions/33200704/use-stringtokenizer-to-count-frequency-of-each-word