10 Replies - 481 Views - Last Post: 07 February 2011 - 09:17 AM Rate Topic: -----

#1 treesap526  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 06-December 10

Search program not finding words

Posted 06 February 2011 - 12:13 PM

Hey guys!

I'm using Windows Forms to build a program that has a textbox and a send button. You type words into the text box and it checks the words against a text file to see if the text file contains certain key words to perform a function.

I don't understand why my code is acting up. Here's my code and I'll explain what it's doing after:

 private void SendButton_Click(object sender, EventArgs e)
        {
            //**READ FROM TEXT BOX 1 INTO _commandWords ARRAY AND CLEAR BOX**//
            String _command = textBox1.Text;
            String[] _commandWords = _command.Split(new string[] { Environment.NewLine, " "}, StringSplitOptions.None);
            textBox1.Clear();

            String[] _Matches;
            _Matches = new String[_commandWords.Length];
            int _commandNumber = 0;
            while (_commandNumber < _commandWords.Length)
            {
                int _matchesNumber = 0;
                while (_matchesNumber < _Matches.Length)
                {
                   String _tempMatches = CompareCommands(_commandWords[_commandNumber]);
                   if (_tempMatches != "No match found" && _tempMatches != "No match Found")
                   {
                       int _matchFound = 0;
                       _Matches[_matchFound] = _tempMatches;
                       MessageBox.Show(_tempMatches);
                       _matchFound++;
                   }
                   else
                   { MessageBox.Show("No match found here"); }
                    _matchesNumber++;
                }
                _commandNumber++;
                int _matchesNumber2 = 0;
                while (_matchesNumber2 < _Matches.Length)
                {
                    MessageBox.Show(_Matches[_matchesNumber2]);
                    _matchesNumber2++;
                }
            }

           
        }



 public string CompareCommands(String _givenCommand)
        {
            String _s = " ";
            using (StreamReader rdr = File.OpenText(@"C:\Users\Slade\Desktop\thecommands.txt"))
            {
                _s = rdr.ReadToEnd();
                rdr.Close();
            }
            int _number = 0;
            String[] _theCommandsFile = _s.Split(new string[] {Environment.NewLine, " "}, StringSplitOptions.None);
            String _commandMatch = "No match Found";
            while (_number < _theCommandsFile.Length)
            {
                if (_givenCommand == _theCommandsFile[_number])
                {
                    _commandMatch = _theCommandsFile[_number];
                }
                else
                {
                    _commandMatch = "No match found";
                }
                _number++;
            }
            return _commandMatch;
        }



When I compile the code and run it, it lets me type it, like normal and when I press the send button, it outputs "no match found" even if I type "run" and "run" is in the text file.

Can you let me know what I am doing wrong?

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Search program not finding words

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: Search program not finding words

Posted 06 February 2011 - 12:44 PM

To be honest, I think your really over complicating this for yourself. First off all, I would read the file in once, and only once. They is no need to read it in over and over everytime the user presses a button.

So, once you have read it in, you can split it one newlines and empty space as you have done, and then search it using linq. Here is a barebones example done in the console:

static void Main()
        {
            //get word to search for
            string search = Console.ReadLine();
            
            //this will store the ocntents of the file
            string file = String.Empty;
            
            //read in all the file once and store it in file variable
            using (StreamReader r = new StreamReader("Test.txt")) file = r.ReadToEnd();
            
            //split file into an array, splitting on newlines and spaces just as you did in your code
            string[] words = file.Split(new string[] { Environment.NewLine, " " },StringSplitOptions.RemoveEmptyEntries);
            
            //Then, you can do all sorts with linq...

            //count number of occurences
            int occurences = words.Count((s) => s == search));

            //check whether a single occurence is present
            bool present = words.Any(s => s == search);

            Console.ReadKey(true);
}




Once you have those words split into an array (as you have done yourself already), I would have thought that you could do whatever you want from there with simple linq.

This post has been edited by CodingSup3rnatur@l-360: 06 February 2011 - 12:50 PM

Was This Post Helpful? 0
  • +
  • -

#3 treesap526  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 06-December 10

Re: Search program not finding words

Posted 06 February 2011 - 01:14 PM

I'm not familiar with LINQ and don't know how to use it haha So how do you use it to search two arrays for matching strings and return which words are matches?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: Search program not finding words

Posted 06 February 2011 - 01:31 PM

Here's a clue: if you search the very last command in the file, does it work?
Was This Post Helpful? 0
  • +
  • -

#5 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: Search program not finding words

Posted 06 February 2011 - 01:38 PM

It would, quite literally, be as easy as calling one method! Here is an example:


//our two test string arrays
string[] s1 = new string[] { "JASON", "RUN", "COLD" };

string[] s2 = new string[] { "JOHN", "COLD", "JASON" };

//call intersect() and store in a IEnumerable<string> collection. You now have all the matches in a collection.
IEnumerable<string> matches = s1.Intersect(s2);

//in this example, I print out the matches
foreach (string s in matches) Console.WriteLine(s);



Simples as that!
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: Search program not finding words

Posted 06 February 2011 - 01:42 PM

And I'm sure when that's handed in for his assignment, the teacher will not suspect at all that someone wrote it for him.
Was This Post Helpful? 0
  • +
  • -

#7 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: Search program not finding words

Posted 06 February 2011 - 02:11 PM

View PostJackOfAllTrades, on 06 February 2011 - 08:42 PM, said:

And I'm sure when that's handed in for his assignment, the teacher will not suspect at all that someone wrote it for him.


Hi JackOfAllTrades,

Can I just clarify what is acceptable to post in terms of code in these situations? I ask just because I have seemingly unintentionally 'crossed the line' into giving unacceptable amounts of code a couple of times today... All I showed him in my last post was a method, and an example of use. Is that still going too far? Can you just clarify this please because I don't want to start inadvertantly breaking the rules left, right and centre :)

This post has been edited by CodingSup3rnatur@l-360: 06 February 2011 - 02:13 PM

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: Search program not finding words

Posted 06 February 2011 - 02:18 PM

We would really prefer that full answers to obvious homework questions not be provided. It does no good to the majority of the people seeking these answers, because they're just going to copy and paste them and present them as their own without making the least bit of an attempt at understanding. Better to give them a hint, as I did up-topic, as to where they're going wrong so they are required to put some thought into what they're doing and reason it out, thus becoming actual programmers rather than copy/paste artists, like so many are doing nowadays with the amount of code out there on the Internet for scavenging.

While your LINQ solution is certainly appropriate in the real world, it's not likely going to go over well in what is likely an introductory C# course. ;)
Was This Post Helpful? 1
  • +
  • -

#9 treesap526  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 06-December 10

Re: Search program not finding words

Posted 06 February 2011 - 02:43 PM

Jackofalltrades: this is actually a personal project of my own haha I AM in a C# intro course at my college, which got me started in c#, but it's WAYYYY to easy. I'm 3 weeks ahead in the Labs, I've already read most the textbook and I've aced both my exams thus far. I'm doing these extra projects to try to enhance my learning experience. I taught myself some C++ a summer ago, and just recently read a book on Java, so that's why it's so easy to me, since C# is derived from the best of both C++ and Java. Like I said, anything I post on here is for personal projects haha the C# course I'm in isn't challenging enough. Our course sticks to console programs, no GUI's or windows forms :( I was upset when I found that out haha

CodingSup3rnatur@l-360 - That actually makes sense the way you put it the second time haha Thanks!!
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Search program not finding words

Posted 07 February 2011 - 08:41 AM

Quote

Our course sticks to console programs, no GUI's or windows forms I was upset when I found that out haha


There's a really good reason for that. Jumping head first into GUIs is a good way to learn how to use a programming framework without really understanding it. This forum is literally filled with questions by people who did just that, and have absolutely no idea how C# or .NET actually works. They think that because they know how to drag buttons and text boxes on a builder and double click them, that they know how to program.

Learning the basics of the language without the distractions of a GUI or any external libraries is quite important. Don't get too far ahead of yourself, or you'll teach yourself bad habits.

Quote

haha


Small piece of personal advice: the experts on this forum are professionals. We do this kind of stuff for a living. Try not to post like you're a 15 year old girl txting her BFF, and people will respect you more.
Was This Post Helpful? 0
  • +
  • -

#11 treesap526  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 06-December 10

Re: Search program not finding words

Posted 07 February 2011 - 09:17 AM

InsertAlias: Thanks for the advice I'll keep that in mind. It's just that I've done console applications in C++ for about a year now and C# is similar in some ways to C++ so once I got a hang on console applications in the class, I wanted to move on so I started teaching myself windows forms. The class isn't ON C#, it's actually an introduction to programming class and uses C# to introduce students to it, so I think that might be why we're sticking with console applications.
But thanks again for the advice InsertAlias, I appreciate it. I actually hadn't thought that older professionals would bother their time with us inexperienced programmers with our incessant questions. I had envisioned teens/young adults who were great programmers posting on here, so I apologize for my assumptions and inappropriate behavior.
Thanks again,
Treesap
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1