java how to use scanner to read and count paragraph

一曲冷凌霜 提交于 2019-12-13 06:19:12

问题


For example, if I have the following lines of text in a file:

this is an example. this is an example.

this is an example. this is an example. this is an example

this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example.

I want to be able to count these lines as 3 paragraphs. Now my code will count this as 4 paragraphs, as it does not know when a paragraph begins and ends.

Scanner file = new Scanner(new FileInputStream("../.../output.txt"));
int count = 0;
while (file.hasNextLine()) { //whilst scanner has more lines
    Scanner s = new Scanner(file.nextLine());
    if(!file.hasNext()){
        break;
    }
    else{
        file.nextLine();
        count++;
    }
    s.close();
}
System.out.println("Number of paragraphs: "+ count);
file.close();

This is what I have so far. It reads lines of text, and treats each line as a single paragraph.

I want it to treat lines of text that don't have any empty line between them as 1 paragraph and count all paragraphs in file.


回答1:


You will not be able to see the spaces or newline characters using Scanner. nextLine() method eliminates the \n's.

You need to use a class and methods that reads the bytes of the file so you can detect the spaces and newline characters.

Try to use read() method of FileInputStream.




回答2:


Scanner probably isn't the best choice if you only want to count lines. BufferedReader is probably better.

    BufferedReader in = new BufferedReader(new FileReader("output.txt"));
    String line = in.readLine();
    int count = 0;
    StringBuilder paragraph = new StringBuilder();
    while (true) {
        if (line==null || line.trim().length() == 0) {
            count++;
            System.out.println("paragraph " + count + ":" + paragraph.toString());
            paragraph.setLength(0);
            if(line == null)
                break;
        } else {
            paragraph.append(" ");
            paragraph.append(line);
        }
        line = in.readLine();
    }
    in.close();
    System.out.println("Number of paragaphs: "+ count);    


来源:https://stackoverflow.com/questions/22155836/java-how-to-use-scanner-to-read-and-count-paragraph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!