Java字数

Java字数,java,Java,我刚开始学习Java,所以我很感谢你的耐心。不管怎样,我正在编写一个字数计算程序,正如你从标题可以看出的那样,我被for循环下面的numWords函数卡住了,我不确定应该将它设置为什么。如果有人能给我指明正确的方向,那就太棒了。非常感谢。这是我到目前为止的所有代码,如果我问的不够具体,请告诉我,这是我的第一篇帖子。再次感谢 import java.util.Scanner; public class WCount { public static void main (String[]

我刚开始学习Java,所以我很感谢你的耐心。不管怎样,我正在编写一个字数计算程序,正如你从标题可以看出的那样,我被for循环下面的
numWords
函数卡住了,我不确定应该将它设置为什么。如果有人能给我指明正确的方向,那就太棒了。非常感谢。这是我到目前为止的所有代码,如果我问的不够具体,请告诉我,这是我的第一篇帖子。再次感谢

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;
            }
        }
    }
}
import java.util.Scanner;
公共类计数{
公共静态void main(字符串[]args){
扫描仪标准输入=新扫描仪(System.in);
字符串[]字数组=新字符串[10000];
int[]字数=新int[10000];
int numWords=0;
while(stdin.hasNextLine()){
字符串s=stdin.nextLine();
String[]words=s.replaceAll(“[^a-zA-Z]”,“”)。toLowerCase().split(“\\s\
+");
for(int i=0;i
如果您的代码仅用于计算单词,那么您根本不需要遍历
单词
数组。换句话说,将您的
for
循环替换为:

numWords += words.length;
最简单的方法可能是查找字母字符序列:

Matcher wordMatch = Pattern.compile("\\w+").matcher(); 
while (wordMatch.find())
    numWords++;
如果您需要对单词执行某些操作(例如将它们存储在计数的映射中),则此方法将使操作更简单:

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);
}
Map wordCount=newhashmap();
Matcher wordMatch=Pattern.compile(“\\w+”).Matcher();
while(wordMatch.find()){
String word=wordMatch.group();
int count=wordCount.getOrDefault(word,0);
wordCount.put(word,count+1);
}

如果您的代码仅用于计算单词,那么您根本不需要遍历
单词
数组。换句话说,将您的
for
循环替换为:

numWords += words.length;
最简单的方法可能是查找字母字符序列:

Matcher wordMatch = Pattern.compile("\\w+").matcher(); 
while (wordMatch.find())
    numWords++;
如果您需要对单词执行某些操作(例如将它们存储在计数的映射中),则此方法将使操作更简单:

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);
}
Map wordCount=newhashmap();
Matcher wordMatch=Pattern.compile(“\\w+”).Matcher();
while(wordMatch.find()){
String word=wordMatch.group();
int count=wordCount.getOrDefault(word,0);
wordCount.put(word,count+1);
}
提示:读取输入

String sentence = stdin.nextLine();
拆线

String [] words = sentence.split(" ");
句子中的字数

System.out.println("number of words in a sentence are " + words.length);
您在评论中提到,您还希望按字母顺序打印该行。为了让您了解Java:

Arrays.sort(words);
提示:读取输入

String sentence = stdin.nextLine();
拆线

String [] words = sentence.split(" ");
句子中的字数

System.out.println("number of words in a sentence are " + words.length);
您在评论中提到,您还希望按字母顺序打印该行。为了让您了解Java:

Arrays.sort(words);

别担心。我们曾经都是初学者

首先,您不需要执行循环,因为“length”属性已经有了它。但是,如果你想练习循环,只要在迭代器每次前进时增加计数器就很容易了

numWords++;

别担心。我们曾经都是初学者

首先,您不需要执行循环,因为“length”属性已经有了它。但是,如果你想练习循环,只要在迭代器每次前进时增加计数器就很容易了

numWords++;

计算字符串
字符串短语
中单词数量的最佳方法是使用String方法split
String[]words=phrase.split(“”)
从字符串中获取字符串数组,并将空格本身作为参数,这将返回一个包含每个不同单词的字符串数组,然后,您可以简单地检查它的长度
单词。长度
,这将为您提供准确的数字。

计算字符串
字符串短语
中单词数量的最佳方法就是使用String方法split
String[]words=phrase.split(“”)从中获取字符串数组
并将其作为参数本身赋予空格,这将返回一个包含每个不同单词的字符串数组,然后您可以简单地检查其长度
单词。长度
将给出确切的数字。

您需要更具体地说明您的问题。我被困在for循环下面的numWords函数中,我不确定应该将它设置为什么?!!!!这是什么意思?为什么要将正则表达式应用于
s
?另外,为什么要在循环中将
numWords
分配给
0
?对不起,让我澄清一下,我对
for(int I=0;I
您没有增加numwords变量,我猜您正在尝试这样做。请尝试用numwords++替换numwords=0,或者按照sprinter在回答中的建议执行,这会更好。您需要更具体地说明您的问题。我被困在for循环下面的numwords函数中,我不确定应该执行什么让它等于?!!!!这是什么意思?为什么要将正则表达式应用于
s
?还有,为什么要在循环中将
numWords
赋值给
0
?对不起,让我澄清一下,我不清楚在
for(int I=0;I
您并没有递增numwords变量,我猜您正试图这样做。请尝试用numwords++替换numwords=0,或者按照sprinter在回答中的建议执行,这会更好,因为并非拆分操作产生的所有字符串都被视为“单词”。然后你不需要循环条件计数。我正在尝试编写一个程序,通过标准输入法对文本中的单词进行计数,然后按字母顺序打印输出,单词旁边有单词计数。我只是在程序的开始阶段,现在正试着让它正确计数单词。感谢所有的帮助help@PM77-1 OP的代码在进行拆分之前删除了所有标点符号和非空格。我假设这意味着剩下的所有内容都是一个单词。除非不是每个字符串都是