Need help searching a text file

Hi,

I need to search a text file for a stamp collection. I am able to write the data, but not search for it and need some help.

The text file is formatted like:

Country (string)
Color (string)
Condition (int)

When filled with data, looks like this:

United States
Red
7
United States
Blue
2
Poland
Blue
2

StreamReader inputFile

inputFile = File.OpenText("StampData.txt");

// my 3 text boxes

country = countryTextBox.Text;
color = colorTTextBox.Text;
condition = conditionTextBox.Text;

while (!inputFile.EndOfStream)
{
stampLabel.Text = (Country + " " + Color + " " + Condition);
}

inputFile.Close();

Open in new window

LVL 3
Computer GuyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Is it one value per line in the text file? Or is it a comma separated file with three values on one line? Will all values always be present for each set? Most likely there will be more then one set in the file I would suggest that you create a class to hold the three values for each set and add them to a collection so that searching would be simpler.
0
KimputerCommented:
Sorry I did it quickly in VB, but I think you can easily translate back to C#. Basically, you were already almost there:

     Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click
        Dim inputfile As StreamReader
        inputfile = File.OpenText("C:\Users\kimputer\Documents\stamp.txt")
        While Not (inputfile.EndOfStream)
            Dim country = inputfile.ReadLine
            Dim color = inputfile.ReadLine
            Dim condition = inputfile.ReadLine
            If (country = countryTextBox.Text) AndAlso (color = colorTTextBox.Text) AndAlso (condition = conditionTextBox.Text) Then
                MsgBox("Found!")
                Exit While
            Else
                MsgBox("Not found")
            End If
        End While
    End Sub

Open in new window


Please note, this looks more like a programmer's beginner assignment. Because the longer the text file, the longer it takes for this search to end because of the looping (even with the shortcircuit).
A database style solution would prove more worthwile in the long run (and when your listings grow).
0
Computer GuyAuthor Commented:
It will be just like the file one per line.

All values will be present.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Fernando SotoRetiredCommented:
NOT FOR POINTS

I have re-written @Kimputer' VB code to C# wich is working code.
StreamReader inputfile = null;
inputfile = File.OpenText(@"C:\Users\kimputer\Documents\stamp.txt");

while (!inputfile.EndOfStream)
{
    string country = inputfile.ReadLine();
    string color = inputfile.ReadLine();
    string condition = inputfile.ReadLine();

    if ( country == countryTextBox.Text && color == colorTTextBox.Text && condition == conditionTextBox.Text ) 
    {
        stampLabel.Text = country + " " + color + " " + condition + " was found";
        break;
    }
    else
    {
        stampLabel.Text = countryTextBox.Text + " " + colorTTextBox.Text + " " + conditionTextBox.Text + " was NOT found";
    }
}

Open in new window

0
Computer GuyAuthor Commented:
Hi,
Thanks!

I tried this for or but it still turned no results

country or color or condition

 if ( country == countryTextBox.Text || color == colorTTextBox.Text || condition == conditionTextBox.Text ) 

Open in new window

0
Fernando SotoRetiredCommented:
Hi Computer Guy;

The code that I updated and the modifications you made to it does work. A couple of things. The code as posted and with the modifications you made will find the first value of the three country, color, or condition. If three values are provided in the TextBox's it will find the first value that matches one of the three. The other and more importantly the comparisons are case sensitive so if in the file you have United States but in the TextBox for country the user entered united states then you will not find anything.
0
KimputerCommented:
use Strings.Lcase() on both the left and right side on the == statement to solve case sensitivity.
0
Fernando SotoRetiredCommented:
Hi Computer Guy;

This version of the solution will compare all value with lower case string values.

StreamReader inputfile = null;
inputfile = File.OpenText(@"C:\Users\kimputer\Documents\stamp.txt");

while (!inputfile.EndOfStream)
{
    string country = inputfile.ReadLine();
    string color = inputfile.ReadLine();
    string condition = inputfile.ReadLine();
    string tbCountry = countryTextBox.Text.ToLower();
    string tbColor = colorTTextBox.Text.ToLower();
    string tbCondition = conditionTextBox.Text.ToLower();

    if (country.ToLower() == tbCountry || color.ToLower() == tbColor || condition.ToLower() == tbCondition) 
    {
        stampLabel.Text = country + " " + color + " " + condition + " was found";
        break;
    }
    else
    {
        stampLabel.Text = country + " " + color + " " + condition + " was NOT found";
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
@Kimputer

the statement Strings.Lcase() is not supported in C# code. The equivalent of it is StringVariableName.ToLower().
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.