13 Replies - 582 Views - Last Post: 19 September 2013 - 10:54 AM Rate Topic: -----

#1 Wizmeister  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 17-October 12

Question in searching array

Posted 17 September 2013 - 11:29 AM

Application that reads contents of two files into arrays ( did this i think ),
The user should be able to enter a girls, boys or both name and the app. should display messages
indicating where the names were found or not in the array.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace NameSearch
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            // Close the form
            this.Close();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            // Clear text boxes and give focus
            txtBoy.Clear();
            txtGirl.Clear();

            // Focus
            txtGirl.Focus();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            // Ah, now the fun starts.
            // Constant for amount of names
            const int SIZE = 400;

            // Create array to hold all the names
            double[] names = new double[SIZE];

            // Stream reader to read text files
            StreamReader read;

            // Counter variable for loop
            int index = 0;

            // open the first file
            read = File.OpenText("GirlNames.txt");

            // Read the contents into the array
            while (index < names.Length && !read.EndOfStream)
            {
                names[index] = double.Parse(read.ReadLine());
                index++;
            }

            // Close the file
            read.Close();

            // Open the second file
            read = File.OpenText("BoyNames.txt");

            // Read the contents into the array
            while (index < names.Length && !read.EndOfStream)
            {
                names[index] = double.Parse(read.ReadLine());
                index++;
            }

            // Close the second file
            read.Close();

            //values used in the loop
            // here should be the search array
            // the thing is i have 2 text boxes (txtboy and txtGirl) that the user can search 
            // the goal is to either find the name in the array and tell user or not find it and tell user
            // 400 names are in the array, 200 girl and 200 boy
            // one search to find the name, even if the user uses both text boxes at once. and one can be blank
            


            
        }

    }
}



Ive tried looking up methods for searching but couldn't find one, i honestly have no idea where to start with this.

Is This A Good Question/Topic? 0
  • +

Replies To: Question in searching array

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9277
  • View blog
  • Posts: 34,789
  • Joined: 12-June 08

Re: Question in searching array

Posted 17 September 2013 - 11:31 AM

If you are reading in a file you have a collection of strings for each line, right? Then you can use a forloop to cycle through each and compare against the input given.
Was This Post Helpful? 0
  • +
  • -

#3 Wizmeister  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 17-October 12

Re: Question in searching array

Posted 17 September 2013 - 11:40 AM

I did stumble across this example in my book, it would work if there was a single text box, but since there is 2 in total, I can't see how to incorporate it.

 while (!found && index < "arrayname".Length)
   {
     if("arrayname"[index] == value) // This right here is what the user value would be
                                     // only, i have 2 text boxes that one could be blank
     {
       found = true;
       position = index;
     }
      
     index++;
 } 

This post has been edited by Wizmeister: 17 September 2013 - 11:40 AM

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9277
  • View blog
  • Posts: 34,789
  • Joined: 12-June 08

Re: Question in searching array

Posted 17 September 2013 - 11:43 AM

You know you can combine two 'IF' statements into one with the use of an 'or' or 'and', right?
Was This Post Helpful? 1
  • +
  • -

#5 Wizmeister  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 17-October 12

Re: Question in searching array

Posted 17 September 2013 - 11:50 AM

So, try,

if ("array"[index] == txtgirl || txtboy *Substitute with variables*)
{
....
}

Was This Post Helpful? 1
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9277
  • View blog
  • Posts: 34,789
  • Joined: 12-June 08

Re: Question in searching array

Posted 17 September 2013 - 11:56 AM

That's the gist...
Was This Post Helpful? 0
  • +
  • -

#7 Wizmeister  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 17-October 12

Re: Question in searching array

Posted 17 September 2013 - 01:36 PM

That just made me feel like.. wow..
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9277
  • View blog
  • Posts: 34,789
  • Joined: 12-June 08

Re: Question in searching array

Posted 17 September 2013 - 01:45 PM

The power of the boolean.
Was This Post Helpful? 1
  • +
  • -

#9 Curtis Rutland  Icon User is online

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


Reputation: 4498
  • View blog
  • Posts: 7,850
  • Joined: 08-June 10

Re: Question in searching array

Posted 17 September 2013 - 03:39 PM

Don't get discouraged. We all start somewhere. As long as you're willing to learn from it and do better next time, it's a net positive.
Was This Post Helpful? 0
  • +
  • -

#10 Wizmeister  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 17-October 12

Re: Question in searching array

Posted 19 September 2013 - 09:46 AM

So, I tried to implement a search, im getting an error thrown "Operator '||' cannot be applies ot operands of type 'bool' and 'double'" I tried googling this with no luck.

Here's what I have :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace NameSearch
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            // Close the form
            this.Close();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            // Clear text boxes and give focus
            txtBoy.Clear();
            txtGirl.Clear();

            // Focus
            txtGirl.Focus();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            // Ah, now the fun starts.
            // Constant for amount of names
            const int SIZE = 400;

            // Create array to hold all the names
            double[] names = new double[SIZE];

            // Stream reader to read text files
            StreamReader read;

            // Counter variable for loop t oadd contents
            int index = 0;

            // open the first file
            read = File.OpenText("GirlNames.txt");

            // Read the contents into the array
            while (index < names.Length && !read.EndOfStream)
            {
                names[index] = double.Parse(read.ReadLine());
                index++;
            }

            // Close the file
            read.Close();

            // Open the second file
            read = File.OpenText("BoyNames.txt");

            // Read the contents into the array
            while (index < names.Length && !read.EndOfStream)
            {
                names[index] = double.Parse(read.ReadLine());
                index++;
            }

            // Close the second file
            read.Close();

            double girlName = double.Parse(txtGirl.Text);   // txt to the variable for searching
            double boyName = double.Parse(txtBoy.Text);     // txt to the variable for searching
            int counter = 0;                                // used to step through array
            bool found = false;                             // flag indicating search results

            while (!found && index < names.Length)
            {

                if (names[counter] == girlName || boyName)

                {
                    found = true;
                
                }
            
            }
            
            


            
        }

    }
}

Was This Post Helpful? 0
  • +
  • -

#11 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9277
  • View blog
  • Posts: 34,789
  • Joined: 12-June 08

Re: Question in searching array

Posted 19 September 2013 - 09:50 AM

085	                if (names[counter] == girlName || boyName)

I am guessing you want 'names[conters]' to be eitehr 'girlname' or 'boyname'? You need to be explicit and ask that.

names[counter] == girlName || names[counter] == boyName


Basically it is getting confused on the order of operations and your intent.
Was This Post Helpful? 0
  • +
  • -

#12 Wizmeister  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 17-October 12

Re: Question in searching array

Posted 19 September 2013 - 10:31 AM

I was told to change it to 2 seperate arrays and then one method for the search:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace NameSearch
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            // Close the form
            this.Close();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            // Clear text boxes and give focus
            txtBoy.Clear();
            txtGirl.Clear();

            // Focus
            txtGirl.Focus();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            // Ah, now the fun starts.
            // Constant for amount of names
            const int SIZE = 200;

            // Stream reader to read text files
            StreamReader read;

            // Counter variable for loop to add contents
            int index = 0;
                                                                            // Create array to hold all the names
            string[] Girlnames = new string[SIZE];
            string[] Boynames = new string[SIZE];

            read = File.OpenText("GirlNames.txt");                          //Opens the Girl names file
            
            while (index < Girlnames.Length && !read.EndOfStream)           //Array for holding girl names     
            {
                Girlnames[index] = read.ReadLine();
                index++;
            }         
            read.Close();                                                   // Close the girl names file



            read = File.OpenText("BoyNames.txt");                           // Open the boys file

            
            while (index < Boynames.Length && !read.EndOfStream)            // Read the contents into the array
            {
                Boynames[index] = read.ReadLine();
                index++;
            }           
             read.Close();                                                  // Close the second file
        }

        private int SequentialSearch(string[] myArray, string name)
        {
            bool found = false;  // Flag indicating search results
            int index = 0;       // Used to step through the array
            int position = -1;   // Position of value, if found

            // Search the array.
            while (!found && index < myArray.Length)
            {
                if (myArray[index] == name)
                {
                    found = true;
                    position = index;
                }

                index++;
            }

            // Return 
            return position;

        }// end of method


    }
}



my issue is now passing the variables :P into the method, considering i have two text boxes for input and that both can be used. i have no idea. i can take method out if needed.

This post has been edited by Wizmeister: 19 September 2013 - 10:33 AM

Was This Post Helpful? 0
  • +
  • -

#13 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9277
  • View blog
  • Posts: 34,789
  • Joined: 12-June 08

Re: Question in searching array

Posted 19 September 2013 - 10:44 AM

I don't follow - what?
Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland  Icon User is online

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


Reputation: 4498
  • View blog
  • Posts: 7,850
  • Joined: 08-June 10

Re: Question in searching array

Posted 19 September 2013 - 10:54 AM

Well, you have two arrays and two text boxes, right? I'm confused as to what you're trying to do. Are you supposed to figure out which one had text and search against that, or fill both in and search both arrays?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1