Java 如何在单独的行中逐字显示句子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19578417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:28:17  来源:igfitidea点击:

How to show sentence word by word in a separate line

javatext-segmentation

提问by Insane

The sentence String is expected to be a bunch of words separated by spaces, e.g. “Now is the time”. showWords job is to output the words of the sentence one per line.

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

It is my homework, and I am trying, as you can see from the code below. I can not figure out how to and which loop to use to output word by word... please help.

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

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);
    }

}

回答by nhgrif

Java's Stringclass has a replacemethod which you should look into. That'll make this homeworkpretty easy.

Java 的String类有一个replace您应该研究的方法。这将使这homework很容易。

String.replace

字符串替换

回答by Simon Forsberg

Since this is a homework question, I will not give you the exact code but I want you to look at the method splitin the String-class. And then I would recommend a for-loop.

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

Another alternative is to replace in your String until there are no more spaces left (this can be done both with a loop and without a loop, depending on how you do it)

另一种选择是在你的字符串中替换,直到没有更多的空格(这可以用循环和不循环来完成,这取决于你如何做)

回答by Rami

Update

更新

Use the split method of the String class to split the input string on the space character delimiter so you end up with a String array of words.

使用 String 类的 split 方法在空格字符分隔符上拆分输入字符串,这样您最终会得到一个字符串字符串数组。

Then loop through that array using a modified for loop to print each item of the array.

然后使用修改后的 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);
            }
        }

    }

回答by Ravi Thapliyal

Using regex you could use a one-liner:

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

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

with the added benefit that multiple spaces won't leave blank lines as output.

额外的好处是多个空格不会留下空行作为输出。



如果您需要更简单的StringString方法方法,您可以将其split()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);



如果您需要更简单的方法(直到StringString索引)以及更多关于您自己的代码行;您需要将代码包装在一个循环中并稍微调整一下。

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);

回答by mau

You're on the right path. Your showWords method works for the first word, you just have to have it done until there are no words.

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

Loop through them, preferably with a while loop. If you use the while loop, think about when you need it to stop, which would be when there are no more words.

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

To do this, you can either keep an index of the last word and search from there(until there are no more), or delete the last word until the sentence string is empty.

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