bdg
bdg

Reputation: 465

searching for strings in a text file using foreach loops

so I am trying to search a file for some specific strings these strings are stroed in a list and are called universities, Courses and UGPG I am using a Streamreader to load the file in.

the issue I am having is that after the first foreach loop has executed the remaining searches I want to complete return N/a as if the strings are not present in the text file. however I know they are in the text file.

Is there a reason for this or a better way to code this?

my code is below.

any help would be greatly appreciated.

validdirectory = new DirectoryInfo(path);
Vfiles = validdirectory.GetFiles("*.txt");
foreach (FileInfo file in Vfiles)
{
    //reads the file contents
    bool Stepout = false;
    bool nextouterloop = false;
    using (StreamReader ReadMessage = new StreamReader(file.FullName))
    {
        String MessageContents = ReadMessage.ReadToEnd();
        Message_Viewer.Text = MessageContents;
        foreach (string Uni_Name in Universities)
        {
            if (MessageContents.Contains(Uni_Name))
            {
                Display_Uni.Text = Uni_Name;
            }
        }

        foreach (string course in Courses)
        {
            if (MessageContents.Contains(course))
            {
                Display_Course.Text = course;
            }
            Display_Course.Text = "N/A";
        }

        if (MessageContents.Contains("Postgraduate"))
        {
            Display_UGPG.Text = "Postgraduate";
        }
        else if (MessageContents.Contains("Undergraduate"))
        {
            Display_UGPG.Text = "Undergraduate";
        }
        Display_UGPG.Text = "N/A";
    }
}

Upvotes: 2

Views: 1738

Answers (2)

user2936735
user2936735

Reputation: 11

Here is a possible solution without the complicated foreach loops:

        if (Universities.Select(p => MessageContents.Contains(p)).Any())
        {
            Display_Uni.Text = Uni_Name;
        }
        else if (Courses.Select(p => MessageContents.Contains(p)).Any())
        {
            Display_Course.Text = course;
        }
        else if (MessageContents.Contains("Postgraduate"))
        {
            Display_UGPG.Text = "Postgraduate";
        }
        else if (MessageContents.Contains("Undergraduate"))
        {
            Display_UGPG.Text = "Undergraduate";
        }
        else
        {
            Display_UGPG.Text = "N/A";
        }

Upvotes: 0

Steve
Steve

Reputation: 216243

Remove the assignement of N/A inside the loop and let it run until completition.
At the end you could just test the content of the textboxes to see if your loops have found something and, if not, set the N/A text

    foreach (string course in Courses)
    {
        if (MessageContents.Contains(course))
            Display_Course.Text = course;
    }
    if (MessageContents.Contains("Postgraduate"))
        Display_UGPG.Text = "Postgraduate";
    else if (MessageContents.Contains("Undergraduate"))
        Display_UGPG.Text = "Undergraduate";

    if(string.IsNullOrWhitespace(Display_Course.Text))
        Display_Course.Text = "N/A";

    if(string.IsNullOrWhitespace(Display_UGPG.Text ))
        Display_UGPG.Text  = "N/A";

By the way, having you used arrays or lists for the universities and courses I suppose that you want to see all the matching names. Actually, your code writes always the last course and university found in the textboxes overwriting the previous name found.
You should change the line that set the Text property with a call to AppendText (perhaps adding also a newline if the textboxes are multiline = true)

....
Display_Uni.AppendText(Uni_Name + Environment.NewLine);
...
Display_Course.AppendText(course + Environment.NewLine);

Upvotes: 6

Related Questions