1

I'm trying to use a while loop to search an array, using a users search input, at the minute I have listed one word film titles into a standard text file, and then the while loop keeps searching the file until it is found, and then it is output on the console.. But I have the problem that the operator "==" can't be applied to string variables? How would I fix this? Thanks a lot! And merry Christmas, here Is my code:

        //Declare variables
        int iOneWordTitle= 0;
        string sSearch;

        //Declare array
        const int iFilm = 7;
        string[] sOneWordTitle = new string[iFilm];

        //Add heading to console
        Console.WriteLine("List of one word film titles");
        Console.WriteLine();

        //ask user what they want to search for
        Console.WriteLine("What film would you like to search for?");
        sSearch = Console.ReadLine();

        //Read the film names from the datafile
        using (StreamReader sr = new StreamReader("filmnames.txt"))
        {
            while (iOneWordTitle < iFilm)
            {
                sOneWordTitle[iOneWordTitle] = (sr.ReadLine());
                iOneWordTitle++;

                if (sSearch == sOneWordTitle)
                {
                    Console.WriteLine(sSearch + " was found at position " + iOneWordTitle);
                }

                else
                {
                    Console.WriteLine("The film was not found");
                }
            }

        }
3
  • 1
    if (sSearch == sOneWordTitle) compares a string to a string[]. You may want to figure out what you really want to compare there. Commented Dec 21, 2013 at 18:38
  • I want it so that if the search is equal to the film, then to output it on the console Commented Dec 21, 2013 at 18:40
  • 1
    By doing this, you are comparing the string against the array object, not the strings contained within the array. Commented Dec 21, 2013 at 18:42

3 Answers 3

2
  1. Instead of using StreamReader, you can easily read all the lines in a text file using File.ReadAllLines()

  2. You are also printing The film was not found 7 times. If you prefer to print it only once, you can keep a bool variable to indicate it wasn't found.

  3. you pre-configured the number of titles in the file to be 7. What if there can be more titles in the file?

So, I think you might want to change your code a little:

        int iOneWordTitle = 0;
        string sSearch;

        //Add heading to console
        Console.WriteLine("List of one word film titles");
        Console.WriteLine();

        //ask user what they want to search for
        Console.WriteLine("What film would you like to search for?");
        sSearch = Console.ReadLine();

        //Read all titles from the text file
        string[] titles = File.ReadAllLines("filmnames.txt");

        //Search the array
        bool found = false;
        foreach (string title in titles)
        {
            if (title.Equals(sSearch))
            {
                Console.WriteLine(sSearch + " was found");
                found = true;
            }
        }

        //If the title wasn't found, print not found message
        if (!found)
            Console.WriteLine("The film was not found");

if you still want to print position it was found on, you can convert your foreach loop to a regular loop:

        for (int iOneWordTitle = 0; iOneWordTitle < titles.Count(); iOneWordTitle++)
        {
            if (titles[iOneWordTitle].Equals(sSearch))
            {
                Console.WriteLine(sSearch + " was found at position " + iOneWordTitle);
                found = true;
            }
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, the first method seems to work great for finding the film, I'm a little confused though how I would implement this for loop into it to find the position of the film on the list, could you provide any further help? Thanks :)
2

It should be

if (sSearch == sOneWordTitle[iOneWordTitle])

instead of

if (sSearch == sOneWordTitle)

In your version you are comparing a string with a string array rather than the array index value.

I'd probably also give thought to your variable names.

3 Comments

The iOneWordTitle++; would need to be moved below the comparison, or the comparison will be done to the next (so far unset) array slot.
Yeah that kind of solves it, but Now it's just saying the film was not found when I type in a correct film name, any idea where I may have gone wrong? Thanks
Try comparing the strings as lowercase or use String.Equals and StringComparison.CurrentCultureIgnoreCase
2

It needs to be something like this:

if (sSearch == sOneWordTitle[iOneWordTitle])

Instead of doing it like this here:

 if (sSearch == sOneWordTitle)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.