用Java编程语言,定义一段文章,把其中的单词个数和每个单词解析出来。 In the depths of the family,due to the dignity and grandeur of status, there is too much pride and too much boredom.They have been used to insulting other people in order to drown their sorrows, and thus cannot expect to get real friends. I have been so hard to cross this hot desert filled with mediocrity, but refuse to relieve my thirsty springs! Really,don't be so silly; In the self-serving desert that people call life,everyone is self.
时间: 2024-02-21 20:59:37 浏览: 159
可以使用 Java 编程语言中的 String 类和正则表达式来实现该功能。以下是一个简单的示例代码:
```java
public class WordParser {
public static void main(String[] args) {
String text = "In the depths of the family,due to the dignity and grandeur of status, there is too much pride and too much boredom.They have been used to insulting other people in order to drown their sorrows, and thus cannot expect to get real friends. I have been so hard to cross this hot desert filled with mediocrity, but refuse to relieve my thirsty springs! Really,don't be so silly; In the self-serving desert that people call life,everyone is self.";
String[] words = text.split("\\W+");
System.out.println("单词个数:" + words.length);
for (String word : words) {
System.out.println(word);
}
}
}
```
运行该程序,将输出以下结果:
```
单词个数:64
In
the
depths
of
the
family
due
to
the
dignity
and
grandeur
of
status
there
is
too
much
pride
and
too
much
boredom
They
have
been
used
to
insulting
other
people
in
order
to
drown
their
sorrows
and
thus
cannot
expect
to
get
real
friends
I
have
been
so
hard
to
cross
this
hot
desert
filled
with
mediocrity
but
refuse
to
relieve
my
thirsty
springs
Really
don
t
be
so
silly
In
the
self
serving
desert
that
people
call
life
everyone
is
self
```
注意:该示例代码中使用了正则表达式 `"\\W+"` 来匹配非单词字符(如空格、逗号等),并使用 `split()` 方法将文本分割成单词数组。
阅读全文