Java 有没有办法println枚举并列出字符串中的单词?

Java 有没有办法println枚举并列出字符串中的单词?,java,arrays,string,counting,Java,Arrays,String,Counting,我正在尝试编写一个java程序,该程序将计算一个声明语句中的单词数,然后将该语句分解为单词,以便列出带有数值的单词并显示单词。我已经解决了总数问题,但我似乎无法将句子中的单词分解,然后按时间顺序列出。我可以用文字,但不能用文字 我已经探索了Java烹饪书和其他地方,以找到解决方案,但我只是不太了解它。正如我所说,我可以让字符计数,我可以对单词进行计数,但是我不能让单个单词在单独的行上打印,并在字符串中使用数字值进行计数 public class MySentenceCounter { p

我正在尝试编写一个java程序,该程序将计算一个声明语句中的单词数,然后将该语句分解为单词,以便列出带有数值的单词并显示单词。我已经解决了总数问题,但我似乎无法将句子中的单词分解,然后按时间顺序列出。我可以用文字,但不能用文字

我已经探索了Java烹饪书和其他地方,以找到解决方案,但我只是不太了解它。正如我所说,我可以让字符计数,我可以对单词进行计数,但是我不能让单个单词在单独的行上打印,并在字符串中使用数字值进行计数

public class MySentenceCounter {
    public static void main(String[] args) {
        String sentence = "This is my sentence and it is not great";

        String[] wordArray = sentence.trim().split("\\s+");
        int wordCount = wordArray.length;
        for (int i=0; i < sentence.length(  ); i++)
            System.out.println("Char " + i + " is " + sentence.charAt(i)); 
        //this produces the character count but I need it to form words, not individual characters.

        System.out.println("Total is " + wordCount + " words.");
    }
}

迭代您创建的
wordArray
变量,而不是for循环中的原始
语句
字符串:

public class MySentenceCounter {
  public static void main(String[] args) {
    String sentence = "This is my sentence and it is not great";
    String[] wordArray = sentence.trim().split("\\s+");
    // String[] wordArray = sentence.split(" "); This would work fine for your example sentence
    int wordCount = wordArray.length;
    for (int i = 0; i < wordCount; i++) {
      int wordNumber = i + 1;
      System.out.println(wordNumber + " " + wordArray[i]);
    }
    System.out.println("Total is " + wordCount + " words.");
  }
}

使用IntStream而不是for循环的更优雅的解决方案:

import java.util.stream.IntStream;

public class ExampleSolution
{
    public static void main(String[] args)
    {
        String sentence = "This is my sentence and it is not great";

        String[] splitted = sentence.split("\\s+");
        IntStream.range(0, splitted.length)
                .mapToObj(i -> (i + 1) + " " + splitted[i])
                .forEach(System.out::println);

        System.out.println("Total is " + splitted.length + " words.");
    }
}

尝试避免太多的复杂性,下面的方法就可以了

public class MySentenceCounter {
    public static void main(String[] args) {
        String sentence = "This is my sentence and it is not great";
        int ctr = 0;
        for (String str : sentence.trim().split("\\s+")) {
            System.out.println(++ctr + "" + str) ;
         } 
         System.out.println("Total is " + ctr + " words.");
    }
}

如何迭代
wordArray
而不是
句子的字母
?使用“-String”拆分将无法处理多个空格或制表符。我想应该改用“\\s+”。是的,但是示例字符串的开头或结尾没有多个空格或额外的空格(出于同样的原因,我删除了
trim()
)。我会把它改回OP的状态。
import java.util.stream.IntStream;

public class ExampleSolution
{
    public static void main(String[] args)
    {
        String sentence = "This is my sentence and it is not great";

        String[] splitted = sentence.split("\\s+");
        IntStream.range(0, splitted.length)
                .mapToObj(i -> (i + 1) + " " + splitted[i])
                .forEach(System.out::println);

        System.out.println("Total is " + splitted.length + " words.");
    }
}
public class MySentenceCounter {
    public static void main(String[] args) {
        String sentence = "This is my sentence and it is not great";
        int ctr = 0;
        for (String str : sentence.trim().split("\\s+")) {
            System.out.println(++ctr + "" + str) ;
         } 
         System.out.println("Total is " + ctr + " words.");
    }
}