This question already has an answer here:
这个问题在这里已有答案:
- how to count the exact number of words in a string that has empty spaces between words? 9 answers
如何计算单词之间空格为空格的字符串中的确切单词数? 9个答案
I've looked all over for an answer for my question, but all of the questions are either about how many words are in each String or aren't what I'm looking for.
我一直在寻找我的问题的答案,但所有问题都是关于每个字符串中有多少单词或者不是我正在寻找的单词。
So far I've read in a file using Scanner and I separated it into 3 strings using newline as the delimiter and put them into an array.
到目前为止,我已经使用Scanner读取了一个文件,我使用换行符作为分隔符将它分成3个字符串并将它们放入数组中。
What I want to do is to count how many words are in each String and then have the output be something similar to String 1 contains 3 two-letter words, 4 three-letter words, 6 five-letter words
.
我想要做的是计算每个字符串中有多少单词然后输出类似于字符串1包含3个双字母单词,4个三字母单词,6个五个字母单词。
I honestly just have no idea how to do it. I'm not asking for you to write the code for me, just how to go about it.
老实说,我根本不知道该怎么做。我不是要求你为我编写代码,只是如何去做。
Thanks in advance!
提前致谢!
1 个解决方案
#1
Start with one of the solutions you found for counting the number of words in a string (that actually identifies the words). Define an array of integers, and use it to keep track of how many words of a given length you can seen so far. Modify the word counter to increment the appropriate counter for a given word's length where it increments the overall count of words.
从您为计算字符串中的单词数量(实际标识单词)找到的解决方案之一开始。定义一个整数数组,并使用它来跟踪到目前为止您可以看到的给定长度的单词数。修改单词计数器,为给定单词的长度增加适当的计数器,增加单词的总数。
#1
Start with one of the solutions you found for counting the number of words in a string (that actually identifies the words). Define an array of integers, and use it to keep track of how many words of a given length you can seen so far. Modify the word counter to increment the appropriate counter for a given word's length where it increments the overall count of words.
从您为计算字符串中的单词数量(实际标识单词)找到的解决方案之一开始。定义一个整数数组,并使用它来跟踪到目前为止您可以看到的给定长度的单词数。修改单词计数器,为给定单词的长度增加适当的计数器,增加单词的总数。