【问题标题】:Spliting line to array of words将行拆分为单词数组
【发布时间】:2017-03-31 07:39:47
【问题描述】:

我在 txt.file 中找到最长和最短的单词。 但我总是拿结果

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

代码:

        List<String> lines = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
        String max = "", min = "BlaBlaBlaBlaBlaBlaBla";
        // We take a single line
        for(String line: lines){
            // Break the next line through the regular to an array of words
            List<String> words = Arrays.asList(line.split("\\P{Punct}\\P{Space}"));
            String tempMax = Collections.max(words);
            max = max.length() < tempMax.length() ? tempMax : max;
            String tempMin = Collections.min(words);
            min = min.length() > tempMin.length() ? tempMin : min;
        }

        textArea.setText(String.format(
                "File Path: %s\n" +
                        "The longest word: %s\n" +
                        "The shortest word: %s\n" +
                        "Task complete...", fileName, max, min));

给个提示)

【问题讨论】:

  • 那有什么问题?
  • 尝试用"[\\p{P}\\p{S}\\p{Zs}\t]+"分割字符串。如果没有帮助,请定义“单词”。也许更好的方法是匹配"(?&gt;\\p{L}\\p{M}*+)+(?:[-'](?&gt;\\p{L}\\p{M}*+)+)*"
  • 您自己做了什么来发现错误?从Pattern 的Javadoc 开始,您会看到字符类是用小写的p 定义的。例如\p{Punct} 而不是 \P{Punct}
  • 如果您提供输入文件的样本数据,我们可以更好地帮助您。

标签: java regex swing split


【解决方案1】:

问题似乎在这里:

String tempMax = Collections.max(words);

Collections.max 返回给定集合的最大元素

对于List&lt;String&gt;最大元素将为您提供列表中按字母顺序排列最后的单词。

例如在这个列表中:

new String[] {"food", "zz", "abcdef", "zoo"}

它将找到"zz" 作为最大元素。

此外,您用于拆分 wors \\P{Punct}\\P{Space} 的正则表达式不正确。

  • \\P{Punct}\\P{Space} 表示非标点词后跟一个空格。

在您的问题下方有一些非常有用的 cmets 建议了正确的拆分方式。

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

【讨论】:

  • 一个好的解决方案是Collections.max(words, Comparator.comparingInt(String::length))
  • 即使你的答案是正确的。这不是 OP 问题的答案,为什么 maxmin 的值是空字符串。 ;-)
  • @SubOptimal:这也取决于文件内容。另请注意,正如 cmets 中指出的那样,OP 的拆分正则表达式 \\P{Punct}\\P{Space} 不正确。
  • 同意。有多个点需要由 OP 审查。因此,我问已经做了什么来找到错误。
【解决方案2】:

使用的正则表达式模式不正确,还使用 ​​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));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多