Java 计算错误的段落

Java 计算错误的段落,java,text-files,Java,Text Files,因此,我希望我的程序计算文本文件中的段落数,但不幸的是,我最后只差1个数字。当我一直得到5分时,我需要4分的答案。全文如下: Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we

因此,我希望我的程序计算文本文件中的段落数,但不幸的是,我最后只差1个数字。当我一直得到5分时,我需要4分的答案。全文如下:

Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in
Liberty, and dedicated to the proposition that all men are created equal. 

Now   we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so
dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a
portion of that field, as a final resting place for those who here gave their lives that that nation might
live. It is altogether fitting and proper that we should do this. 

But,    in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground.
The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add
or detract. The world will little note, nor long remember what we say here, but it can never forget
what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which
they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great
task remaining before us -- that from these honored dead we take increased devotion to that cause for which
they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have
died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of
the people, by the people, for the people, shall not perish from the earth.



Abraham Lincoln
November 19, 1863     
这是我的代码:

public static void main(String[] args) {
    String input;
    Scanner kbd = new Scanner(System.in);
    System.out.print("Enter the name of the input file: ");
    input = kbd.nextLine();
    try
    {
        // Set up connection to the input file
        Scanner input1 = new Scanner(new FileReader(input));

        // Set up connection to an output file
        PrintWriter output=newPrintWriter(newFileOutputStream(input".txt"));

        // initialize the counter for the line numbers
        int lineNum = 0;
        int words = 0;
        int characters = 0;
        int paragraphs = 0;
        // as long as there are more lines left in the input file
        // read from the input file, and copy to the output file
        while (input1.hasNextLine())
            {
        // read a line from the input file
        String line;

        line = input1.nextLine();
        // copy the line to the output file, adding a
        // line number at the front of the line
        output.println(line + "\n");
        // increment the line counter
        lineNum++;
        //Section for counting the words
         boolean word = false;
            for (int i = 0; i < line.length(); i++) {
                //checks for letters and counts as word till it finds a space then checks for a letter again.
                if (!Character.isWhitespace(line.charAt(i)) && !word) {

                    words++;
                    word = true;
                }
                else if (Character.isWhitespace(line.charAt(i)) && word){
                    word = false;

                }
            }   
            characters += line.length();
            paragraphs += getPara(line);
            }


        // close the files
        input1.close();
        output.close(); 
        System.out.println("Lines: " + lineNum);
        System.out.println("Words: " + words);
        System.out.println("Characters: " + ((characters)));
        System.out.println("Paragraphs: " + paragraphs);



    }
    catch(FileNotFoundException e)
    {
        System.out.println("There was an error opening one of the files.");
    }

}

public static int getPara(String line){  
    int count = 0; 
    boolean p = false;

    if (line.isEmpty() && !p){
        count++;
        p = true;
    }
    else if (!line.isEmpty() && p){
        p = false;
    }

    return count;
}

}
publicstaticvoidmain(字符串[]args){
字符串输入;
扫描仪kbd=新扫描仪(System.in);
System.out.print(“输入输入文件名:”);
输入=kbd.nextLine();
尝试
{
//设置与输入文件的连接
Scanner input1=新扫描仪(新文件读取器(输入));
//设置与输出文件的连接
PrintWriter输出=newPrintWriter(newFileOutputStream(输入“.txt”);
//初始化行号的计数器
int lineNum=0;
int字=0;
整数字符=0;
int=0;
//只要输入文件中还有更多的行
//从输入文件读取,然后复制到输出文件
while(input1.hasNextLine())
{
//从输入文件中读取一行
弦线;
line=input1.nextLine();
//将该行复制到输出文件,添加
//线路前端的线路编号
output.println(第+行“\n”);
//增加行计数器
lineNum++;
//计算字数的部分
布尔字=假;
对于(int i=0;i
您的代码计算的是空行,而不是段落。输入文件中的空行会增加段落数

在这种情况下,假设和定义是关键。如何在需求文档中定义段落?您是否可以假设段落是以换行符结尾的单行,或者您是否需要考虑段落中的换行符?如果是前者,则
nextLine()
返回的所有非空行都将是段落。如果是后者,那么只有在非空行后面有空行或EOF时,才需要添加段落计数


在任何情况下,使用语言实用程序(如清点字数)都能更好地为您服务,除非您需要手动清点。

段落是一行,后面跟着一个空行。该方法不读取每一行,只读取第一行