Java lambda表达式接受来自用户的句子

Java lambda表达式接受来自用户的句子,java,arrays,lambda,expression,Java,Arrays,Lambda,Expression,我正在编写一个程序,它使用lambda表达式接受来自用户的字符串,将其转换为所有小写字母并删除标点符号,然后按字母顺序列出唯一的单词。我无法让我的程序接受用户的句子,也无法删除标点符号。我尝试过使用.replaceAll(),但我遇到了一个错误,因此我一定做得不对,或者使用的代码不正确。感谢您的帮助。以下是我目前掌握的代码: //I added my own string just to see if the code I have works. public static void main(

我正在编写一个程序,它使用lambda表达式接受来自用户的字符串,将其转换为所有小写字母并删除标点符号,然后按字母顺序列出唯一的单词。我无法让我的程序接受用户的句子,也无法删除标点符号。我尝试过使用
.replaceAll()
,但我遇到了一个错误,因此我一定做得不对,或者使用的代码不正确。感谢您的帮助。以下是我目前掌握的代码:

//I added my own string just to see if the code I have works.
public static void main(String[] args) {
    String[] strings = {"The brown fox chased the white rabbit."};


    System.out.printf("Original strings: %s%n", Arrays.asList(strings));


    Stream<Map.Entry<String, Long>> uniqueWords = Arrays.stream(strings)
         .map(String::toLowerCase)
         //remove punctuation?
         .collect(Collectors.groupingBy(w -> w, Collectors.counting()))
         .entrySet().stream()
         .filter(e -> e.getValue() == 1)
         .distinct();
    System.out.println("Unique words in Alphabetical Order: "+ uniqueWords);
}
//我添加了自己的字符串,只是想看看我的代码是否有效。
公共静态void main(字符串[]args){
String[]strings={“棕色的狐狸追赶白兔。”};
System.out.printf(“原始字符串:%s%n”,Arrays.asList(字符串));
Stream uniqueWords=Arrays.Stream(字符串)
.map(字符串::toLowerCase)
//删除标点符号?
.collect(Collectors.groupingBy(w->w,Collectors.counting())
.entrySet().stream()
.filter(e->e.getValue()==1)
.distinct();
System.out.println(“按字母顺序排列的唯一单词:“+唯一单词”);
}

您可以使用
扫描仪
接受用户输入,使用
nextLine()
方法从控制台接受一行输入。然后,您可以将句子转换为小写,然后创建
数组
。您可以使用正则表达式
\\W+
,它将在任何非单词字符上拆分

而且,您使
过于复杂。您只需使用和创建按字母顺序排列的唯一值流:

Scanner in = new Scanner(System.in);
String[] strings = in.nextLine().toLowerCase().split("\\W+");
System.out.printf("Original strings: %s%n", Arrays.asList(strings));
Arrays.stream(strings).distinct().sorted().forEach(System.out::println);
样本输入/输出:


你试过使用
扫描仪吗?
?我试过使用扫描仪,但当我提示用户输入文本时,不知怎么搞砸了数组。它没有正确地存储字符串。
This This is. a sentence with duplicate words words, and punctuation!!
Original strings: [this, this, is, a, sentence, with, duplicate, words, words, and, punctuation]
a
and
duplicate
is
punctuation
sentence
this
with
words