Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将行拆分为单词数组_Java_Regex_Swing_Split - Fatal编程技术网

Java 将行拆分为单词数组

Java 将行拆分为单词数组,java,regex,swing,split,Java,Regex,Swing,Split,我正在txt.file中查找最长和最短的单词。 但我总是以结果为准 File Path: there it's ok. The longest word: *empty* The shortest word: *empty* Task complete... 代码: List lines=Files.readAllLines(path.get(fileName)、StandardCharsets.UTF_8); 字符串max=“”,min=“blablabla”; //我们用一条线 用于(字符

我正在txt.file中查找最长和最短的单词。 但我总是以结果为准

File Path: there it's ok.
The longest word: *empty*
The shortest word: *empty*
Task complete...
代码:

List lines=Files.readAllLines(path.get(fileName)、StandardCharsets.UTF_8);
字符串max=“”,min=“blablabla”;
//我们用一条线
用于(字符串行:行){
//将下一行从规则行打断到一个单词数组
List words=Arrays.asList(line.split(\\P{Punct}\\P{Space}));
字符串tempMax=Collections.max(单词);
最大=最大长度()tempMin.length()?tempMin:min;
}
textArea.setText(String.format(
“文件路径:%s\n”+
“最长单词:%s\n”+
“最短单词:%s\n”+
“任务完成…”,文件名,最大值,最小值);

给出提示)

问题似乎在这里:

String tempMax = Collections.max(words);
返回给定集合的最大元素

对于
列表
最大元素将为您提供按字母顺序排列在列表最后的单词

例如,在这个列表中:

new String[] {"food", "zz", "abcdef", "zoo"}
它将发现
“zz”
为最大元素

此外,用于拆分工作
\\p{Punct}\\p{Space}
的正则表达式不正确

  • \\P{Punct}\\P{Space}
    表示后跟空白的非标点词
在你的问题下面有一些非常有用的评论,建议正确的分割方法


至少使用:
[\\p{p}\\p{ZS}]+
进行拆分。

问题似乎在这里:

String tempMax = Collections.max(words);
返回给定集合的最大元素

对于
列表
最大元素将为您提供按字母顺序排列在列表最后的单词

例如,在这个列表中:

new String[] {"food", "zz", "abcdef", "zoo"}
它将发现
“zz”
为最大元素

此外,用于拆分工作
\\p{Punct}\\p{Space}
的正则表达式不正确

  • \\P{Punct}\\P{Space}
    表示后跟空白的非标点词
在你的问题下面有一些非常有用的评论,建议正确的分割方法


至少使用:
[\\p{p}\\p{ZS}]+
进行拆分。

使用的正则表达式模式不正确,还可以使用Comparator获得结果

List<String> lines = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
String max = "", min = "";
List<String> words = new ArrayList<String>();
// We take a single line
for(String line: lines){
    // Break the next line through the regular to an array of words
    words.addAll(Arrays.asList(line.split("[\\p{Punct}\\s]+")));
}

Comparator<String> stringComparator = new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
        return Integer.valueOf(o1.length()).compareTo(Integer.valueOf(o2.length()));
    }
};

max = Collections.max(words,stringComparator);
min = Collections.min(words,stringComparator);

textArea.setText(String.format("File Path: %s\n" +"The longest word: %s\n" +"The shortest word: %s\n" +"Task complete...", fileName, max, min));
List lines=Files.readAllLines(path.get(fileName)、StandardCharsets.UTF_8);
字符串max=“”,min=“”;
List words=new ArrayList();
//我们用一条线
用于(字符串行:行){
//将下一行从规则行打断到一个单词数组
addAll(Arrays.asList(line.split([\\p{Punct}\\s]+”));
}
比较器stringComparator=新比较器(){
@凌驾
公共整数比较(字符串o1、字符串o2){
返回Integer.valueOf(o1.length()).compareTo(Integer.valueOf(o2.length());
}
};
max=Collections.max(单词、字符串比较器);
min=Collections.min(单词、字符串比较器);
textArea.setText(String.format(“文件路径:%s\n”+“最长单词:%s\n”+“最短单词:%s\n”+“任务完成…”,文件名,最大值,最小值));

使用的正则表达式模式不正确,还使用Comparator获得结果

List<String> lines = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
String max = "", min = "";
List<String> words = new ArrayList<String>();
// We take a single line
for(String line: lines){
    // Break the next line through the regular to an array of words
    words.addAll(Arrays.asList(line.split("[\\p{Punct}\\s]+")));
}

Comparator<String> stringComparator = new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
        return Integer.valueOf(o1.length()).compareTo(Integer.valueOf(o2.length()));
    }
};

max = Collections.max(words,stringComparator);
min = Collections.min(words,stringComparator);

textArea.setText(String.format("File Path: %s\n" +"The longest word: %s\n" +"The shortest word: %s\n" +"Task complete...", fileName, max, min));
List lines=Files.readAllLines(path.get(fileName)、StandardCharsets.UTF_8);
字符串max=“”,min=“”;
List words=new ArrayList();
//我们用一条线
用于(字符串行:行){
//将下一行从规则行打断到一个单词数组
addAll(Arrays.asList(line.split([\\p{Punct}\\s]+”));
}
比较器stringComparator=新比较器(){
@凌驾
公共整数比较(字符串o1、字符串o2){
返回Integer.valueOf(o1.length()).compareTo(Integer.valueOf(o2.length());
}
};
max=Collections.max(单词、字符串比较器);
min=Collections.min(单词、字符串比较器);
textArea.setText(String.format(“文件路径:%s\n”+“最长单词:%s\n”+“最短单词:%s\n”+“任务完成…”,文件名,最大值,最小值));

那有什么问题吗?试着用
“[\\p{p}\\p{s}\\p{Zs}\t]+”
拆分字符串。如果没有帮助,请定义“单词”。也许更好的方法是匹配
“(?>\\p{L}\\p{M}*+)+(?:[-'](?>\\p{L}\\p{M}*+)*”
。您自己做了什么来查找错误?从Javadoc开始,您将看到字符类是用小写的
p
定义的。e、 g.
\p{Punct}
而不是
\p{Punct}
。如果您提供输入文件的示例数据,我们可以更好地帮助您。那么有什么问题吗?请尝试使用
“[\\p{p}\\p{s}\\p{Zs}\t+”
拆分字符串。如果没有帮助,请定义“单词”。也许更好的方法是匹配
“(?>\\p{L}\\p{M}*+)+(?:[-'](?>\\p{L}\\p{M}*+)*”
。您自己做了什么来查找错误?从Javadoc开始,您将看到字符类是用小写的
p
定义的。e、 g.
\p{Punct}
而不是
\p{Punct}
。如果您提供输入文件的示例数据,我们可以更好地帮助您。一个好的解决方案是
Collections.max(words,Comparator.comparingit(String::length))
。即使您的答案是正确的。这不是OP问题的答案,为什么
max
min
的值是空字符串@次优:这也取决于文件内容。还要注意,OP的拆分正则表达式
\\P{Punct}\\P{Space}
是不正确的,正如在comments.Agree中指出的那样。OP需要审查多个要点。因此我询问了已经做了哪些工作来发现错误。一个好的解决方案是
Collections.max(words,Comparator.comparingit(String::length))
。即使您的答案也是正确的。这不是OP问题的答案,为什么
max
的值