简体   繁体   English

Java字数统计

[英]Java Word Count

I am just starting out in Java so I appreciate your patience. 我刚开始使用Java,因此感谢您的耐心配合。 Anyways, I am writing a word count program as you can tell by the title, I am stuck at the numWords function below the for loop, I am not sure what I should set it equal to. 无论如何,我正在编写一个字数统计程序,正如您可以按标题说明的那样,我被困在for循环下面的numWords函数中,我不确定应该将其设置为等于什么。 If someone could set me in the right direction that would be awesome. 如果有人可以让我朝正确的方向前进,那就太好了。 Thank you. 谢谢。 Here is all of my code thus far, let me know if I not specific enough in what I am asking, this is my first post. 这是到目前为止的所有代码,如果我对所要询问的内容不够具体,请告诉我,这是我的第一篇文章。 Thanks again. 再次感谢。

import java.util.Scanner;
public class  WCount {

    public static void main (String[] args) {
        Scanner stdin = new Scanner(System.in);

        String [] wordArray = new String [10000];
        int [] wordCount = new int [10000];
        int numWords = 0;

        while(stdin.hasNextLine()){
            String s = stdin.nextLine();
            String [] words = s.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s\
+");
            for(int i = 0; i < words.length; i++){
                numWords = 0;
            }
        }
    }
}

If your code is intended to just count words, then you don't need to iterate through the words array at all. 如果您的代码仅用于计算单词,则根本不需要遍历words数组。 In other words, replace your for loop with just: 换句话说,将您的for循环替换for

numWords += words.length;

Most likely a simpler approach would be to look for sequences of alpha characters: 最可能的一种更简单的方法是查找字母字符序列:

Matcher wordMatch = Pattern.compile("\\w+").matcher(); 
while (wordMatch.find())
    numWords++;

If you need to do something with the words (such as store them in a map to a count) then this approach will make that simpler: 如果您需要对单词进行某些操作(例如将它们存储在映射到计数中),则此方法将使操作变得更简单:

Map<String,Integer> wordCount = new HashMap<>();
Matcher wordMatch = Pattern.compile("\\w+").matcher(); 

while (wordMatch.find()) {
    String word = wordMatch.group();
    int count = wordCount.getOrDefault(word, 0);
    wordCount.put(word, count + 1);
}

Don't worry. 不用担心 We were all beginners once. 我们曾经都是初学者。

First of all, you don't need to do the loop because "length" attribute already has it. 首先,您不需要执行循环,因为“ length”属性已经具有该循环。 But, if you want to practice with loops is so easy as increasing the counter each time the iterator advances and that's it. 但是,如果您想使用循环练习很容易,则每次迭代器前进时就增加计数器,仅此而已。

numWords++;

Hint: Read the input 提示:读取输入

String sentence = stdin.nextLine();

Split the string 分割字符串

String [] words = sentence.split(" ");

Number of words in a sentence 句子中的单词数

System.out.println("number of words in a sentence are " + words.length);

You mentioned in comments that you would also like to print the line in alphabetical order. 您在评论中提到您还希望按字母顺序打印该行。 For that Java got you covered: 对于Java,您可以了解:

Arrays.sort(words);

计算String String phrase中单词数量的最好方法就是使用String方法split String[] words = phrase.split(" ")从字符串数组中获取一个String数组,并将其本身作为参数,这是最好的方法将返回一个包含每个单词的String数组,然后您可以简单地检查它的长度words.length ,这将为您提供确切的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM