Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

integrated search algorithm problem 1

Status
Not open for further replies.

srd1984

Programmer
Dec 12, 2004
11
GB
Hi, I am creating a web browser that has a search engine component that links automatically to google news.

it contains a textbox as the search component, and on pressing enter the search will be carried out.

The way I am approaching it is google automatically has a first part of the link that everything has which is " and after this it will say the text so for example if the search was "hello" then this "hello" would be appended to the end of the "=" and if it was "hello world" it would append "hello+world".

My code works when one word is placed in the text box, albeit in a slightly cheating way, if the word is hello it has the link as " so although there is a "+" at the end, it still only searches for the "hello".

However, when I have more than one word it causes this error:

"Index and length must refer to a location within the string.
Parameter name: length"

I have been trying to figure this out for hours and am sure its somethin simple.

I have posted the main bits of my code below, if any1 would be kind enough to read through it and tell me why it is doing this I would be most grateful:

----------------------------------------------------------
My code:
----------------------------------------------------------

public void goToURL(String theURL)
{
try
{
Cursor.Current = Cursors.WaitCursor;
Object o = null;
axWebBrowser2.Navigate(theURL, ref o, ref o, ref o, ref o);
}
finally
{
Cursor.Current = Cursors.Default;
}
}

private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
String googleSearch = "
if(e.KeyCode == Keys.Enter)
{
parse(textBox1.Text);

for (int i = 0; i<listOfWords.Count; i++)
{
googleSearch = googleSearch + listOfWords + "+";
}

goToURL(googleSearch);
listOfWords.Clear();
}
}

private void parse(String textToParse)
{
// If word has a space
if(textToParse.IndexOf(" ") != -1)
{
// Find index of space
int index = textToParse.IndexOf(" ");
// Parse out word before space
String word = textToParse.Substring(0,index);
// Add to listOfWords
listOfWords.Add(word);
// Parse out word after first word
String endWord = textToParse.Substring(index, textToParse.Length);
// Trim any spaces at start or end of word
endWord.Trim();
// Repeat process
parse(endWord);
}

// If indexOf(" ") equals null then this is the last word so its added to list
else
{
listOfWords.Add(textToParse);
}
 
The error is caused by the parse() function.
This is the line with problem (the 2nd index is not right):
Code:
String endWord = textToParse.Substring(index, textToParse.Length);
Replace the the parse() by the following:
Code:
private void parse(String textToParse)
{
	string [] words = textToParse.Split(' ');
	foreach (string o in words)
	{
		if (o.Length>0)
			listOfWords.Add(o);
	}
}
There is no need for recursivity and even this one is no need.
Where you need to split the text, call the Split() function and use directly the array of returned strings without storing in an ArrayList.
Also, put the following:
Code:
String googleSearch = "[URL unfurl="true"]http://www.google.com/news?hl=en&ned=us&q=";[/URL]
as a private member instead to have it allocated on KeyDown() handler.
-obislavu-
 
I suggest using string.Join in textBox1_KeyDown and textToParse.Split in parse. This will eliminate any indexing headaches in your code.
 
Thank you for ure response :)

however I am still very confused. I am quite new to c#, coming from only knowing intermediate java.

I have replaced the parse() method however could u explain what foreach (string o in words) means/does?

Also I do not know what you mean by this line

"Where you need to split the text, call the Split() function and use directly the array of returned strings without storing in an ArrayList"

My arraylist is called listOfWords and u have added to it with the code:

"if (o.Length>0)
listOfWords.Add(o)"

so i dont understand what u mean by not needing to store in an ArrayList

Thanks once again
 
Ok sorry I understand where I have gone wrong and udnerstand u were just merely explaining what ure code does.


Thank you!!!
 
Oh wait next small minor problem is to do with where I am clearing the arraylist

If I type "hello" and search that, then add " world" to the end, e.g. so it says "hello world" and I havent deleted the first "hello", it will actually search "hello hello world" i.e. its not clearing the arraylist.

Any ideas?

Thanks again
 
You might try this instead, it will eliminate a lot of code:

Code:
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
   //Replace the spaces in the search text entered
   //by the user with plus signs
   if(e.KeyCode == Keys.Enter)
      goToURL(
         "[URL unfurl="true"]http://www.google.com/news?hl=en&ned=us&q="[/URL] +
         textBox1.Text.Replace(" ", "+"));                
}
 
If you still to use an ArrayList in the parse() function then seems to be a need to clear the array at the parse level:
Code:
private void parse(String textToParse)
{
   listOfWords.Clear();
    string [] words = textToParse.Split(' ');
    foreach (string o in words)
    {
        if (o.Length>0)
            listOfWords.Add(o);
    }
}
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top