【问题标题】:Java Print string input as an array with each word in string on a new lineJava将字符串输入作为数组打印,字符串中的每个单词在新行上
【发布时间】:2018-01-24 15:47:04
【问题描述】:

我需要按照程序输出输入的任何字符串,每个单词都在新行上,并计算总单词数。到目前为止,这会在新行上输出每个字母。我使用 text.split 来计算单词并尝试将其添加到我的 for 循环中,但它不起作用,可能是因为 for 循环是 Integer 但我找不到如何添加它。

public class TextTest {

    public static void main(String[] args) {
        String text = Input.getString("Text: ");
        String[] separated = text.split(" ");//separates by spaces 

        do {
            if (text.isEmpty()) {
                System.out.println("empty text");
            } else {
                for (Integer position = 0; position < text.length(); position++) {

                    System.out.println(text.charAt(position));
                }
            }
            int WordCount = separated.length;
            System.out.println("number of words: " + WordCount);
        } while (Repeat.repeat());

【问题讨论】:

  • "但它不起作用" 你需要更详细。有错误吗?例外?输出错误?它在做什么?
  • 正如@csmckelvey 建议的那样,如果您添加可能的错误或输出 - 您的问题可能会被认为是有益且格式良好的

标签: java


【解决方案1】:
public static void main(String[] args) {
        String text = "THIS IS A SAMPLE TEXT";
        String[] separated = text.split(" ");//separates by spaces 

            if (text.isEmpty()) {
                System.out.println("empty text");
            } else {
                for (Integer position = 0; position < separated.length; position++) {

                    System.out.println(separated[position]);
                }
            }
            int WordCount = separated.length;
            System.out.println("number of words: " + WordCount);
    }

您的代码不起作用,因为您使用文本来输出单词,而不是您创建并分隔其中的单词的新数组。这会起作用,因为每个单词都位于新创建的数组中的索引中。希望我有所帮助!

【讨论】:

    【解决方案2】:
    public static void main(String[] args) {
        String text = "hello I am here, how are  you?";
        String[] separated = text.split(" ");//separates by spaces
    
        for (String word : separated) {
            if (!word.trim().isEmpty()) {
                System.out.println(word);
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      使用 Lambdas 就像下面这样简单:

      public static void main(String[] args) {
          String text = "THIS IS A SAMPLE TEXT";
          String[] splitted = text.split(" ");
      
          System.out.println("Number of words: " + Stream.of(splitted).count());
          Stream.of(splitted).forEach(System.out::println);
      }
      

      【讨论】:

      • 投反对票。首先,这不会编译。 text.split(" ") 后面应该有一个分号。其次,String::split 需要一个参数并且不会像这样编译。第三,您的解决方案没有执行请求的操作。
      • @ifly6 你是完全正确的,谢谢你的提示。之前有另一个解决方案并动态更改它,因此产生了这个错误。我已经更新了我的代码以创建正确的版本。
      【解决方案4】:

      您可以像下面这样更改for

      String[] splits = text.split("\\s+"); // split by one or more spaces.
      for (String split: splits){
          System.out.println(split);
      }
      System.out.println("Word Count: " + splits.length);
      

      字数将被拆分计数。此外,您不需要考虑空拆分,因为 RegExp 将处理单词被多个空格分隔的情况。

      【讨论】:

      • 这可能是 OP 问题的最简单直接的解决方案。
      【解决方案5】:

      使用 Java 8 流,就可以了。它

      String text = "THIS IS A SAMPLE TEXT";
      long count = Stream.of(text.split("\\s+")) // split based on whitespace
              .peek(System.out::println) // print each one
              .count(); // count them
      System.out.println("Word count: " + count); // print the count
      

      【讨论】:

        猜你喜欢
        • 2020-05-20
        • 2015-07-30
        • 2013-06-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多