1

I am experiencing an Error within nouns[i] = SearchKeywords(words, sentences); every time I run my program. What should I do?

*I edited to include the SearchKeywords method. I am trying to find all words that have "a" and "an" before them.

Error is, Error Cannot implicitly convert type 'string[]' to 'string'

    string[] SearchKeywords(List<string> keywords, string[] sentence){

    int[] location = new int[sentence.Length];

    foreach (string word in keywords)
    {
        for (int i = 0; i < sentence.Length;i++ )
        {
            string[] nouns = new string[i];
            if (String.IsNullOrEmpty(sentence[i]))
            {
                return null;
            }
            else
            {
                location[i] = sentence[i].IndexOf(word);

                nouns[i] = sentence[i].Substring(location[i]);

                if (nouns[i].Contains(word)) return nouns;
            }
        }

    }
    return sentence;

   string[] checkForIndefinite()
   {
       string input = txtEntry.Text;

       string text = lbltxtOutput.Text;

       Tools tool = new Tools();    

       List<string> words = new List<string>();
       words.Add("an");
       words.Add("a");

       foreach(string sentence in GetWords(text))
       {
           for (int i = 0; i < sentence.Length; i++)
           {
               string[] nouns = new string[i];
               string[] sentences = new string[i];
               sentences[i] = sentence;

               **nouns[i] = SearchKeywords(words, sentences);**

              return nouns;

           }
       }
       return null;
   }
6
  • What is that error? Mention it here. Commented May 21, 2015 at 2:28
  • What Type does SearchKeywords method return? I assume string[]? Commented May 21, 2015 at 2:30
  • 1
    SearchKeywords is returning an array of strings, which you are trying to assign to a (single) string variable. Obviously, you can't do that. It can't really be fixed without knowing what your goal for this code is. Commented May 21, 2015 at 2:33
  • Do you think you could provide compilable code? Commented May 21, 2015 at 2:37
  • I edited to include the SearchKeywords method. I am trying to find all words that have "a" and "an" before them. Commented May 21, 2015 at 2:42

3 Answers 3

2

It seems to me that this is what you need:

string[] SearchKeywords(List<string> keywords, string[] sentence)
{
    return
        sentence
            .Zip(sentence.Skip(1), (w0, w1) => new { w0, w1 })
            .Where(ww => keywords.Contains(ww.w0))
            .Select(ww => ww.w1)
            .ToArray();
}

string[] checkForIndefinite()
{
    var input = txtEntry.Text;
    var text = lbltxtOutput.Text;
    var words = new List<string>() { "an", "a" };
    return SearchKeywords(words, GetWords(text));
}
Sign up to request clarification or add additional context in comments.

6 Comments

This is giving me an error on line .Where(ww => keywords.Contains(w0)) saying that, "The name w0 does not exist in the current context."
Have you got using System.Linq; at the top?
@JohnHodge - It worked just fine for me. I run every answer in LINQPad before posting.
I have using System.Linq;. Here the entire program if you want to take a look: https://dotnetfiddle.net/KL8QeX
For what it's worth, I get the same result in Visual Studio.
|
0

The output of the SearchKeywords function is an string[] and the nouns[i] is a string and simply you cannot assign and string[] value to a string. So You may what to try something like this:

List<string> nouns = new List<string>();

//foreach loop
    //    for loop

        nouns.AddRange(SearchKeywords(words, sentences));

Comments

-1
using System;
using System.Security.Cryptography;
using System.Text;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        string[] indefinites = checkForIndefinite();
        foreach (string s in indefinites)
        {
            Console.WriteLine(s);
        }
    }


    static string[] SearchKeywords(List<string> keywords, string sentence)
    {
        if (String.IsNullOrEmpty(sentence))
        {
            return null;
        }

        List<string> nouns = new List<string>();

        string[] words = sentence.Split(' ');

        foreach (string keyword in keywords)
        {
            for (int i = 0; i < words.Length; i++ )
            {
                if (words[i] == keyword)
                {
                    if (i+1 < words.Length && !nouns.Contains(words[i+1]))
                    {
                        nouns.Add(words[i+1]);
                    }
                }
            }
        }
        return nouns.ToArray();
    }

   static string[] checkForIndefinite()
   {
       string sentence = "This is not an interesting sentence to use to test a program";

       string text = string.Empty;   

       List<string> words = new List<string>();
       words.Add("an");
       words.Add("a");

       return SearchKeywords(words, sentence);

   }
}

This compiles and returns the following:

interesting
program

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.