【问题标题】:Extracting lines with only 1 word?提取只有 1 个单词的行?
【发布时间】:2016-07-06 18:57:42
【问题描述】:

试图只获取其中包含 1 个单词的行。

当前方法得到了正确的结果,但有时输入文件的每个单词之间的行数超过 4 行。所以需要一种方法来只获取其中包含 1 个单词的行。有什么想法吗?

以下是输入文本的示例:

adversary
someone who offers opposition
The students are united by shared suffering, and by a common adversary. 
— New York Times (Nov 10, 2014)
aplomb
great coolness and composure under strain
I wish I had handled it with aplomb. 
— New York Times (May 18, 2014)
apprehensive

所以输出应该是这样的:

adversary
aplomb
apprehensive

这是目前为止的代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Process {

    public static void main(String[] args) {

        String fileNameOutput = "OutputFile.txt";
        String fileName = "InputWords";

        try (BufferedReader bReader = Files.newBufferedReader(Paths.get(fileName))){

            PrintWriter outputStream = new PrintWriter(fileNameOutput); 
            int lineNum = 0;
            String line = null;

            while ( (line = bReader.readLine() ) != null ) {
               lineNum++;

             if ( lineNum % 4 == 0 ) continue;


                outputStream.println(line);


            }
                outputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }



    }

}

感谢您的宝贵时间。


编辑

通过以下建议的修复从控制台获取此错误。

java.nio.charset.MalformedInputException: Input length = 1
    at java.nio.charset.CoderResult.throwException(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.BufferedReader.fill(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at Process.main(Process.java:20)

【问题讨论】:

    标签: java string bufferedreader


    【解决方案1】:

    好吧,而不是

    if ( lineNum % 4 == 0 ) continue;
    

    条件,你可以简单地检查一下你刚才读到的那一行是否包含多个token:

    if (line.split(" ").length > 1) continue;
    

    或

    if (line.indexOf(" ") >= 0) continue;
    

    后一种条件应该比前一种更有效。

    【讨论】:

    • 啊,太棒了,现在试试。干杯!
    • 是的,出错了...(MalformedInputException:输入长度 = 1)
    • @JonathanLaliberte 哪一行代码给出了这个异常?
    • 不完全确定如何找到它。你是这个意思? " 在 Process.main(Process.java:20) "
    • @JonathanLaliberte Process.java 的第 20 行是什么?
    【解决方案2】:

    取决于你对“单词”的定义:

    • 字母序列
    • 任何非空格字符的序列
    • 代表单词的字形(例如中文)

    让我们坚持前两个,并使用正则表达式进行检查,这样我们也可以轻松忽略前导和尾随空格。以下是三种方式:

    if (line.matches("\\s*[a-zA-Z]+\\s*")) // One or more ASCII letters
        outputStream.println(line);
    
    if (line.matches("\\s*\\p{L}+\\s*")) // One or more Unicode letters
        outputStream.println(line);
    
    if (line.matches("\\s*\\S+\\s*")) // One or more non-space characters
        outputStream.println(line);
    

    至于MalformedInputException,是代码页不匹配导致的(StreamDecoder抛出异常)。

    newBufferedReader(path) 以 UTF-8 读取文件,该文件很可能在系统默认代码页中,而不是 UTF-8。

    请改用newBufferedReader(path, Charset.defaultCharset())。

    【讨论】:

    • 谢谢!我试了一下,但仍然在描述中出现错误...知道为什么吗?
    • 是的,newBufferedReader() 以 UTF-8 读取文件,该文件很可能不是 UTF-8,而是系统默认代码页。请改用newBufferedReader(path, Charset.defaultCharset())。
    • 但是代码工作正常,没有任何人在这里建议的任何编辑。所以这不是问题。
    • 由于BufferedReader.readLine()中发生错误,并且没有人告诉您更改该调用,因此该错误不是由我们所说的引起的。
    • 该错误是由—字符引起的,它不是MINUS符号,而是EM DASH。
    【解决方案3】:

    而不是

    if ( lineNum % 4 == 0 ) continue;
    

    只需检查行是否包含空格。

    if(line.trim().contains(" ")) continue;
    

    【讨论】:

    • "String 类型中的方法 contains(CharSequence) 不适用于参数 (char)"
    • 嗯,谢谢,但由于某种原因仍然出现这个该死的错误,即使有这个建议。有什么想法吗?
    【解决方案4】:

    您在 java.io.BufferedReader.readLine(Unknown Source) 处收到一条错误消息,因此找不到输入文件... 尝试更改文件名

    String fileName = "InputWords";
    
    to
    
    String fileName = "InputWords.txt";
    

    【讨论】:

      【解决方案5】:

      工作!需要添加字符集。

         public static void main(String args[]){
              //testAnimal();
               String fileNameOutput = "OutputFile.txt";
                  String fileName = "InputWords.txt";
      
                  Charset cs = Charset.defaultCharset() ;
                  try (BufferedReader bReader = Files.newBufferedReader(Paths.get(fileName), cs)){
      
                      PrintWriter outputStream = new PrintWriter(fileNameOutput); 
                      int lineNum = 0;
                      String line = null;
      
                      while ( (line = bReader.readLine() ) != null ) {
                         lineNum++;
      
                        if (line.split(" ").length > 1) continue;
      
      
                          outputStream.println(line);
      
      
                      }
                          outputStream.close();
      
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
      
      
         }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多