2012-11-05 69 views
5

我正在嘗試從文本文件中計算單詞的數量,即此數字以開始計算。計算文本文件中單詞的數量

這是對字數統計程序的測試。這只是一個測試。如果您的 程序成功運行,則應計算此文件中有30個 單詞。

我使用的StreamReader把一切都從文件轉換成字符串,然後使用.Split方法得到的個別單詞的數量,但我不斷收到錯誤的值當我編譯和運行程序。

using System; 
using System.IO; 

class WordCounter 
{ 
    static void Main() 
    { 
     string inFileName = null; 

     Console.WriteLine("Enter the name of the file to process:"); 
     inFileName = Console.ReadLine(); 

     StreamReader sr = new StreamReader(inFileName); 

     int counter = 0; 
     string delim = " ,."; 
     string[] fields = null; 
     string line = null; 

     while(!sr.EndOfStream) 
     { 
      line = sr.ReadLine(); 
     } 



     fields = line.Split(delim.ToCharArray()); 
     for(int i = 0; i < fields.Length; i++) 
     { 
      counter++; 
     } 
     sr.Close(); 
     Console.WriteLine("The word count is {0}", counter); 
    } 
} 
+1

不同於論壇的網站,我們不使用「謝謝」,或者「任何幫助表示讚賞」,或簽名(因此)。請參閱「[應該'嗨','謝謝',標語和致敬從帖子中刪除?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be請參閱「[應該在標題中包含」標籤?「(http://meta.stackexchange.com/questions/19190/)」), –

回答

1

一對夫婦的提示。

  1. 如果你只有句子「嗨」,你會輸出什麼?
  2. 您的計數器計算是:從0到fields.Length,增量計數器。 fields.Length和你的計數器有什麼關係?
+0

1.當我在文本文件中加入「hi」時,它告訴我單詞計數是1. – user1781027

2

這應該爲你工作:

using System; 
using System.IO; 

class WordCounter 
{ 
static void Main() 
{ 
     string inFileName = null; 

     Console.WriteLine("Enter the name of the file to process:"); 
     inFileName = Console.ReadLine(); 

     StreamReader sr = new StreamReader(inFileName); 

     int counter = 0; 
     string delim = " ,."; //maybe some more delimiters like ?! and so on 
     string[] fields = null; 
     string line = null; 

     while(!sr.EndOfStream) 
     { 
     line = sr.ReadLine();//each time you read a line you should split it into the words 
     line.Trim(); 
     fields = line.Split(delim.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); 
     counter+=fields.Length; //and just add how many of them there is 
     } 


     sr.Close(); 
     Console.WriteLine("The word count is {0}", counter); 
} 

}

+0

爲什麼不'StreamReader.ReadToEnd()'? – neeKo

+0

@ NikoDrašković如果這個文件有1000或10000或10M字怎麼辦?因爲我很久以前就開始使用C語言,所以我絕對不會使用ReadToEnd,這是一種習慣,但是認爲在我可以的情況下讀入文件中的某些內容看看它的塊是不是最好的選擇,這也說明了OP的代碼錯誤 –

+0

你的代碼工作,但我不明白爲什麼當我只輸出fie lds.Length,它給了我3的值。怎麼回事,counter + = fields.Length給了30,當counter初始化爲0時? – user1781027

0

你可能得到一次性的錯誤,嘗試這樣的事情

counter = 0; 
    while(!sr.EndOfStream) 
    { 
     line = sr.ReadLine(); 
     fields = line.Split(delim.ToCharArray()); 
     counter += field.length(); 
    } 

沒有必要當您可以直接獲取數字時迭代數組以計算元素

+0

我給了我的文本文件,該段被分成3行,最後一行只包含「在這個文件中」。當我嘗試Console.WriteLine(線);在我的程序中沒有其他任何東西,它只是輸出「在這個文件中。」你知道它爲什麼只是讀最後一行嗎? – user1781027

+0

啊,k srry我沒有仔細看,它是因爲讀取一次只讀取一行,所以你正在讀取所有的數據,但只計算最後一行讀取 –

+0

我改變了我的代碼,使它現在是 line + = sr.ReadLine +「」; 現在,當我在Console.WriteLine上顯示字符串時,它會輸出整個字符串。但是,當我嘗試顯示fields時,它給了我一個值35。任何想法爲什麼? – user1781027

3

嘗試使用正則表達式,例如:

var count = Regex.Matches(input, @"\b\w+\b").Count(); 
0
//Easy method using Linq to Count number of words in a text file 
/// www.techhowdy.com 
// Lyoid Lopes Centennial College 2018 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace FP_WK13 
{ 
    static class Util 
    { 

     public static IEnumerable<string> GetLines(string yourtextfile) 
     { 
      TextReader reader = new StreamReader(yourtextfile); 
      string result = string.Empty; 
      string line; 
      while ((line = reader.ReadLine()) != null) 
      { 
       yield return line; 
      } 
      reader.Close(); 
     } 



     // Word Count 

     public static int GetWordCount(string str) 
     {   
      int words = 0; 
      string s = string.Empty; 
      var lines = GetLines(str); 

      foreach (var item in lines) 
      { 
       s = item.ToString(); 
       words = words + s.Split(' ').Length; 

      } 

      return words; 

     } 


    } 
} 
+0

不要只放置代碼,請添加一些描述你如何得到這個解決方案。 – Lakmi