I'm trying to make a dictionary game, and I have a text file with about 100,000 words each on their own line. I have this code:

   words = new List<Word>();
   Console.WriteLine("Please wait, compiling words list...");
   TextReader tr = new StreamReader(DICT);
   string line = tr.ReadLine();
   while (line != "" && line != null) {
    words.Add(new Word(line));
    line = tr.ReadLine();
   }
   Console.WriteLine("List compiled with " + words.Count + " words.");

However, it stops at 40510 words. Why is this? And how can I absolve the issue?

Thank you.

有帮助吗?

解决方案

Edit: Sorry; I checked for blank lines in notepad and found none; searching in Notepad++ has found them.

My bad, thank you anyway.

其他提示

Does it just stop or throws exception? Check line variable value in debugger before Console.WriteLine call, probably empty line there.

The problem is your line != "" check. Remove that and it will continue.

The problem seems to be your while{} loop.

I would do something like this:

words = new List<Word>(); 
Console.WriteLine("Please wait, compiling words list..."); 
TextReader tr = new StreamReader(DICT); 
string line;
while((line = tr.ReadLine()) != null)
if(!string.IsNullOrEmpty(line.Trim()))
{ 
 words.Add(new Word(line)); 
} 
Console.WriteLine("List compiled with " + words.Count + " words.");

I haven't tested that, so there could be some errors, but the big thing is that your while{} loop will break on the first blank line instead of just discarding it. In this example, that is corrected, and it will only break when there are no more lines to read.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top