Java Counting # of occurrences of a word in a string

前端 未结 8 1846
再見小時候
再見小時候 2020-12-03 19:20

I have a large text file I am reading from and I need to find out how many times some words come up. For example, the word the. I\'m doing this line by line e

8条回答
  •  生来不讨喜
    2020-12-03 20:12

    Why not run your line through the Java StringTokenizer then you can get the words broken up by not just spaces but also commas and other punctuation. Just run through your tokens and count the occurrence of each "the" or any word you would like.

    It would be very easy to expand this a bit and make a map that had each word as a key and kept a count of each word use. Also you may need to consider running each word through a function to stem the word so you can count a more useful thing then just the words.

提交回复
热议问题