StreamReader not reading to end of text file

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
Hello,
I am trying to load a dictionary<string, string> with questions and answers from a text file.

I have a text file with 2000 questions and answers... each line contains one question and a corresponding answer formatted like this question*answer...
I am using a stream reader to read the file and split the questions from the answers

here is the function to load the dictionary

private  void Load_General_Knowledge()        {         
            using (StreamReader reader = new StreamReader(@"Assets\Questions\RandomQuestions.txt"))
            {
                string Line = "";
                while ((Line = reader.ReadLine()) != null)
                {
                  string[] splitLine = Line.Split('*');
                    if (questions.ContainsKey(splitLine[0]))
                    {
                        return;
                    }
                    else
                    questions.Add(splitLine[0], splitLine[1]);
                  
                }              
            }            
        }


any help is appreciated

Thank You
InkedGFX
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,144
Location
Sydney, Australia
Programming Experience
10+
Um, yeah... and what seems to be the problem? The first obvious issue is that you have a return statement in there. Do you really want to stop reading the file the first time you encounter a duplicate question, or would you actually want to just ignore it and move on to the next? That said, why would you have duplicates anyway?
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
Thank you for your help....I thought a return statement would return program to the beginning of the loop....now I know it doesn't.....I changed the return to a "continue"
now it works as expected....I downloaded a text file what I thought was 2000 questions...come to find out there are only 432 questions.....the first time I ran the loop to
add the questions and answers to the dictionary...I got an error saying that the key was already added to the Dictionary..so I figured there were some dups.......

again , thank you for your help.
-InkedGFX
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,144
Location
Sydney, Australia
Programming Experience
10+
But why do you even need a 'continue'? Why do this:
if (questions.ContainsKey(splitLine[0]))
{
    continue;
}
else
{
    questions.Add(splitLine[0], splitLine[1]);
}
when you can just do this:
if (!questions.ContainsKey(splitLine[0]))
{
    questions.Add(splitLine[0], splitLine[1]);
}
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
yes!

you are absolutley correct..there is no need to write the code the way I have it....much better to check if it doesn't exist and if not then add it.....

I have changed my code again to check if it doesn't exist...thank you

-InkedGFX
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
Stream Reader isnt reading to the end of text file

for some reason this code only reads 428 lines of a 2000 line text file.......

private  void Load_General_Knowledge()
        {
                     using (StreamReader reader = new StreamReader(@"Assets\Questions\RandomQuestions.txt"))
            {
                string Line = "";
                while ((Line = reader.ReadLine()) != null)
                {
                  string[] splitLine = Line.Split('*');
                    if (questions.ContainsKey(splitLine[0]))
                    {
                        return;
                    }
                    else
                    questions.Add(splitLine[0], splitLine[1]);
                   
                }              
            }            
        }


any idea as to why this would be?

Thank You
-InkedGFX
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,144
Location
Sydney, Australia
Programming Experience
10+
Um, why have you completely ignored everything that I've already posted to this thread? I told you not to use if...else and I told you not to use 'return' and you are still using both. If you're going to waste my time then why would I bother? As I have already told you once and will not be telling you a third time, this:
if (questions.ContainsKey(splitLine[0]))
{
    return;
}
else
    questions.Add(splitLine[0], splitLine[1]);
should be this:
if (!questions.ContainsKey(splitLine[0]))
{
    questions.Add(splitLine[0], splitLine[1]);
}
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
thats my fault..I posted the incorrect code.......

here is the correct code I am using..I did take your advice...sorry about that

 {                     using (StreamReader reader = new StreamReader(@"Assets\Questions\RandomQuestions.txt"))
            {
                int counter = 0;
                string Line = "";
                
                while ((Line = reader.ReadLine()) != null)
                {
                  string[] splitLine = Line.Split('*');
                  if (!questions.ContainsKey(splitLine[0]))
                  {
                         counter++;
                    questions.Add("[" + counter.ToString() + "]" + splitLine[0], splitLine[1]);
                  }  
                }              
            }            
        }


this code reads 428 lines of 2000 lines .....I checked the text file...there are no blank lines .......

-InkedGFX
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,144
Location
Sydney, Australia
Programming Experience
10+
In that case, my guess would be that there's a line with no * in it, but that's just a guess. What you should be doing is debugging the code. You often can't see what's wrong with code simply by looking at the code. As it is presumably in this case, it often misbehaves due to specific conditions, which is why you have to actually watch the code in action. That's why the IDE includes a debugger. Place a breakpoint and step through the code. Use the Auto, Locals and Watch windows and other tools to monitor the values of properties and variables and evaluate arbitrary expressions along the way. As soon as you see execution take a direction or data take a value that you didn't expect, you've found an issue.
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
well I have to say THANK YOU!!!!!!!!!

you nailed it...I went through every line of the text file and found 2 lines that didn't have the *
that took me almost an hour..there are over 26000 lines ........

but that was the problem...and solution!!!!!

thank you

-InkedGFX
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
lol...because I didn't want to ask you how to do it :)

-InkedGFX
 
Top Bottom