Question

I'm playing around with C# trying to get the basics down pat, but I'm stumped on this odd error. I'm trying to search through an array of names, and then return the location of any matches. At the moment I'm just printing the name, but I'm able to get the location.

For some reason though, when I type a name I want to search for and hit enter, the program crashes saying "ArraySearch.exe has stopped working". Any idea whats wrong??

Pardon the nooby questions, I'm still new to this language/paradigm :p

Thanks! :)

using System;

public class Test
{
    public static void Main()
    {   
        string[] stringArray = {"Nathan", "Bob", "Tom"};
        bool [] matches = new bool[stringArray.Length];
        string  searchTerm = Console.ReadLine();
        for(int i = 1;i<=stringArray.Length;i++){
            if (searchTerm == stringArray[i - 1]){
                matches[i] = true;
            }
        }
        for(int i = 1;i<=stringArray.Length;i++){
            if (matches[i]){
                Console.WriteLine("We found another " + stringArray[i - 1]);
            }
        }
    }
}
Was it helpful?

Solution

i will reach 3, which will give Out of range exception here:

matches[i] = true;

This would be easy to spot using debugging. (Try to learn using it. Very useful)

matches has 3 elements so the index ranges from 0 to 2.

OTHER TIPS

using System;

public class Test
{
    public static void Main()
    {
        string[] stringArray = { "Nathan", "Bob", "Tom" };
        bool[] matches = new bool[stringArray.Length];
        string searchTerm = "Bob";
        for (int i = 1; i <= stringArray.Length; i++)
        {
            if (searchTerm == stringArray[i - 1])
            {
                matches[i - 1] = true;
            }
        }
        for (int i = 1; i <= stringArray.Length; i++)
        {
            if (matches[i - 1])
            {
                Console.WriteLine("We found another " + stringArray[i - 1]);
            }
        }
    }
}

See the fix above. It was failing because the bool array does not have an item at index 3. 3 is 1 index more than its last index, i.e. 2.

Its indexes are 0,1,2. which in turn store 3 elements just as u require.

When looping through arrays it is best to get used to using the actual indexes. So i would suggest looping from 0 until array.length - 1. That way u have a direct reference to every element in the array.

I would strongly suggest using lambda's for this kind of processing. Much fewer lines and simple to understand.

For example:

using System;
using System.Linq;

namespace ConsoleApplication5
{
    public class Test
    {
            public static void Main()
            {
                string[] stringArray = { "Nathan", "Bob", "Tom" };
                bool exists = stringArray.Any(x => x.Equals("Nathan"));
                Console.WriteLine(exists);
            }
     }
}

The above method call, Any(), does the search for you. There are many options to explore. You can replace Any() with First(), Last() etc. If i were you I would definitely explore the System.Linq library for these kind of operations on collections.

(Remember that an array is also a collection)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top