2015-11-26 27 views
1

我工作的一類項目,統計一個文本文件的字數,行數,字符和段落的總數。到目前爲止,它正在儘可能的話去,但我的性格似乎數由3關,而段落似乎是計算兩個額外的空行,我得到5而不是4字計數項目的缺陷

這是我到目前爲止有:

import java.util.*; 
import java.io.*; 

public class WordStats { 

    /* getWordCount() method will receive a String parameter 
    * and return the total number of words by splitting 
    * the received string into words and increment word count */ 
    public static int getWordCount (String line){ 

     int wordCount = 0; 

     String str [] = line.split((" ")); 
     for (int i = 0; i <str.length; i ++){ 
      if(str[i].length() > 0){ 
       wordCount++; 
      } 
     } 

     return wordCount; 
    } 

    /* getParsCount method receives a string parameter 
    * and returns the total number of paragraphs in 
    * the text file. */ 
    /*public static int getParsCount(String line){ 

     int parCount=0; 
     boolean isText = false; 

     if(!line.isEmpty()){ 
      isText=false; 
      } 

     else { 
       isText=true; 
       parCount++; 

     } 

     return parCount; 
    } 
    */ 

    public static int getParsCount(String line) { 
     boolean isText=false; 
     if (!line.isEmpty()) { 
       if (!isText) { 
        isText = true; 
        return 1; 
       } 
      } 
      else { 
       isText = false; 
      } 

      return 0; 
     } 
    public static void main(String[] args) { 

     try{ 

      int chars =0, words = 1, lines =0, pars=0; 

      // creates new Scanner inFile 
      Scanner inFile = new Scanner(new File("data.txt")); 

      //creates file to write updated data file. 
      PrintWriter outFile = new PrintWriter(new FileOutputStream("dataCopy.txt")); 

      //Loop that sends string variables to methods so long as there is another 
      //line break in the file. 
      while(inFile.hasNextLine()){ 

       String line = inFile.nextLine();// read aline from the input file 

       lines++;      //increment line count 
       chars += (line.length());  //increment char count 
       words += getWordCount(line); //Increment word count 
       pars += getParsCount(line);  // increment paragraph count. 
       outFile.println(line + "\n"); 
      } 

      System.out.println("The number of Characters in the file are: " + chars); 
      System.out.println("The number of Words in the file are: " + words); 
      System.out.println("The number of Lines in the file are: " + lines); 
      System.out.println("The number of Paragraphs in the file are: " + pars); 
      inFile.close(); // closes file input. 
      outFile.close();// closes output file. 
      System.out.print("File Written"); 
     } 

     catch(FileNotFoundException e){ 
      System.out.print("ERROR: CANNOT PROCESS FILE"); 
     } 

    } 

} 

這是輸入文件:

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 

輸出是這樣的:

The number of Characters in the file are: 1495 
The number of Words in the file are: 283 
The number of Lines in the file are: 22 
The number of Paragraphs in the file are: 5 
+1

你的意見是? – Frakcool

+1

「輸出是這樣的:」對於什麼輸入? –

+1

您的計算段落或文本塊的數量的邏輯是有缺陷的。您可能在輸入中有一些額外的換行符,這會導致多餘的段落「出現」。 –

回答

0

以下是您可以對代碼進行的更改,以使其能夠正確計數輸入文件中的段落數或連續的文本塊數。創建被設置爲true如果當前行有內容,並且在一個空行的情況下被設定爲一個false標誌boolean。然後,如果兩個段落被多於一個空行分隔,則多個空行只會被計數一次。另外,輸入文件末尾的額外空行將被忽略。

public class WordStats2 { 

    boolean isText = false; 

    public static int getParsCount(String line) { 
     if (!line.trim().isEmpty()) { 
      if (!isText) { 
       isText = true; 
       return 1; 
      } 
     } 
     else { 
      isText = false; 
     } 

     return 0; 
    } 
} 

由於您從未向我們展示您的輸入,我們只能推測爲什麼字符數也是關閉的。一種可能性是文件末尾的多餘空行再次是罪魁禍首。這些「空行」不是空的,但實際上包含一行或多行行尾字符(Windows中的\r\n,Linux中的\n)。所以你的程序可能會計算這些字符。發佈您的輸入,我可以修改我的答案。

+0

感謝您的建議和答覆。我明白你在說什麼,但是,我認爲我發佈的輸入文件故意錯誤地輸入以糾正這些錯誤。另外,我錯過了從打印到新文件的內容? – mlopman

+1

我不明白你的評論,但看完你的輸入文件後,我可以說我的猜測是正確的。您正在計算空行並將它們視爲段落。這隻適用於a)段落總是由一行和一行分隔的情況,以及b)在最後一段之後你的文件只有一條空行。 –

+1

由於事實並非如此,請試試我的代碼,並告訴我們是否解決了您的問題。至於打印你的輸出,讓我們先讓輸入工作,然後回到那個。 –