views:

55

answers:

4

Hi, 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.

+1  A: 

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

STO
Agreed. Use the debugger !
driis
+1  A: 

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

My bad, thank you anyway.

Motig
Yeah, don't use Notepad for anything programming-related. Switch Notepad++ to be your default.
Randolph Potter
A: 

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

consultutah
A: 

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.

AllenG