简体   繁体   中英

Split a sentence ignoring characters in Java

I Want to write a program that reads one line of input text and breaks it up into words.

The (solution) words should be output one per line. A word is defined to be a sequence of letters.

Any characters in the input that are not letters should be discarded.

For example, if the user inputs the line:

He said, "That’s not a good idea."

then the output of the program should be:

He
said
That
‘s
not
a
good
idea

Simply use a regex

    Pattern pattern = Pattern.compile("[\\w'’]+");
    Matcher matcher = pattern.matcher("He said, \"That’s not a good idea.\"");
    while (matcher.find())
        System.out.println(matcher.group());

Try this:

public class Main {
    public static void main(String[] args) {
        Scanner stdIn = new Scanner(System.in); // user input
        String line = stdIn.nextLine(); // read line
        String[] words = line.split("[^a-zA-Z]+"); // split by all non-alphabetic characters (a regex)
        for (String word : words) { // iterate through the words
            System.out.println(word); // print word with a newline
        }
    }
}

It won't include the apostrophe in the token 's , but I don't know why you included that. It's not a letter, after all, and I read your first bold sentence. I hope the comments help explain how it works. There will be a trailing empty line, but that should be easy for you to fix if you really need to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM