【问题标题】:How to show sentence word by word in a separate line如何在单独的行中逐字显示句子
【发布时间】:2013-11-03 21:32:15
【问题描述】:

句子字符串应该是一堆用空格分隔的单词,例如。 “现在是时候了”。 showWords 的工作是每行输出一个句子的单词。

这是我的作业,我正在努力,正如您从下面的代码中看到的那样。我不知道如何以及使用哪个循环逐字输出...请帮助。

import java.util.Scanner;


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

        System.out.println("Enter the sentence");
        String sentence = in.nextLine();

        showWords(sentence);
}

    public static void showWords(String sentence) {
        int space = sentence.indexOf(" ");
        sentence = sentence.substring(0,space) + "\n" + sentence.substring(space+1);
        System.out.println(sentence);
    }

}

【问题讨论】:

    标签: java text-segmentation


    【解决方案1】:

    您走在正确的道路上。你的 showWords 方法适用于第一个单词,你只需要完成它直到没有单词。

    循环遍历它们,最好使用 while 循环。如果您使用 while 循环,请考虑何时需要它停止,也就是没有更多单词的时候。

    为此,您可以保留最后一个单词的索引并从那里搜索(直到没有更多),或者删除最后一个单词直到句子字符串为空。

    【讨论】:

      【解决方案2】:

      由于这是一个家庭作业问题,我不会给你确切的代码,但我希望你看看String-class 中的方法split。然后我会推荐一个 for 循环。

      另一种选择是在您的字符串中替换,直到没有更多空格为止(这可以通过循环和不循环来完成,具体取决于您的操作方式)

      【讨论】:

      • replace 方法将通过单个方法调用处理所有出现的空格字符。不需要循环。
      • @nhgrif 该方法是的,但还有其他替代方法(例如replaceFirst,或使用像OP似乎已经尝试过的子字符串)。 replace 当然可能是最简单的解决方案。
      • 多个空格怎么办?正则表达式替换是更好的选择
      • @Bohemian 我将把它留给 OP 来决定。这个决定不是我做的。您还可以将连续的两个空格视为中间的“长度为零的单词”。
      • “一个长度为零的单词”...很好的回击 :)
      【解决方案3】:

      使用正则表达式,您可以使用单行:

      System.out.println(sentence.replaceAll("\\s+", "\n"));
      

      还有一个额外的好处,即多个空格不会留下空行作为输出。


      如果您需要更简单的 String 方法,您可以使用 split() 作为
      String[] split = sentence.split(" ");
      StringBuilder sb = new StringBuilder();
      for (String word : split) {
          if (word.length() > 0) { // eliminate blank lines
              sb.append(word).append("\n");
          }
      }
      System.out.println(sb);
      


      如果您需要更简单的方法(低至String 索引)以及更多您自己的代码行;您需要将代码包装在一个循环中并稍微调整一下。
      int space, word = 0;
      StringBuilder sb = new StringBuilder();
      
      while ((space = sentence.indexOf(" ", word)) != -1) {
          if (space != word) { // eliminate consecutive spaces
            sb.append(sentence.substring(word, space)).append("\n");
          }
          word = space + 1;
      }
      
      // append the last word
      sb.append(sentence.substring(word));
      
      System.out.println(sb);
      

      【讨论】:

      • 问题是关于家庭作业的。发布一个直接的答案并不是特别有用。
      【解决方案4】:

      Java 的 String 类有一个 replace 方法,您应该研究一下。这将使homework 变得非常容易。

      String.replace

      【讨论】:

        【解决方案5】:

        更新

        使用 String 类的 split 方法在空格字符分隔符上拆分输入字符串,这样你就得到了一个 String 的单词数组。

        然后使用修改后的 for 循环遍历该数组以打印数组中的每一项。

        import java.util.Scanner;
        
        
            public class Test {
                public static void main(String[] args) {
                    Scanner in = new Scanner(System.in);
        
                    System.out.println("Enter the sentence");
                    String sentence = in.nextLine();
        
                    showWords(sentence);
            }
        
                public static void showWords(String sentence) {
                    String[] words = sentence.split(' ');
                    for(String word : words) {
                     System.out.println(word);
                    }
                }
        
            }
        

        【讨论】:

        • 问题是关于家庭作业的。发布一个直接的答案并不是特别有用。
        猜你喜欢
        • 1970-01-01
        • 2014-06-17
        • 2012-03-26
        • 1970-01-01
        • 2020-07-13
        • 2012-12-19
        • 1970-01-01
        • 1970-01-01
        • 2017-06-23
        相关资源
        最近更新 更多