Alexander. Matt
Alexander. Matt

Reputation: 141

How do I get my If else loop to loop through an array C# .NET

I got a list of 10 items.

when a user puts in a value in "listText" its comparing to the first item in displayArraysString.

Lets say its not the first item in the displayArraysString list, then it doesnt do anything (Because I dont have a loop)

How do I create a loop that will check through my list and display the messagebox once it finds it. I tried with a try catch loop but that didnt work for me.

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

namespace Arrays
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int[] numbers = new int[5];
        List<int> numbersList = new List<int> ();
        string text = System.IO.File.ReadAllText(@"C:Directory\list.txt");

        private void Form1_Load(object sender, EventArgs e)
        {
            //numbers[0] = 12;
            //numbers[1] = 10;
            //numbers[2] = 25;
            //numbers[3] = 10;
            //numbers[4] = 15;
            //numbersList.Add(23);
            //numbersList.Add(32);
            //numbersList.Add(35);
        }
        //Array Print
        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < numbers.Length; i++)
            displayArrays.Text += numbers[i].ToString() + ", ";
        }
        //List Print
        private void button2_Click(object sender, EventArgs e)
         {
            for (int o = 0; o < text.Length; o++)
            {
                displayArraysString.Text += text[o].ToString();

                if (listText.Text == displayArraysString.Text)
                {
                    MessageBox.Show("Found a match!");

                }
                else
                {
                   //Something.
                }




            }
         }
    }
}

enter image description here

enter image description here

Upvotes: 0

Views: 100

Answers (1)

sumngh
sumngh

Reputation: 566

You are trying something wrong here, as the file you have read from the path in string , it will match a single character for listText, so there will be never a match, I did it with string array, to convert the text data into string array of every words in it. If you search now than match will be found for listText.

try this code:

     namespace Arrays
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int[] numbers = new int[5];
        List<int> numbersList = new List<int> ();
        string text = System.IO.File.ReadAllText.Text(@"C:\Directory\list.txt");
        string[] displayStringArrays = null; 

        private void Form1_Load(object sender, EventArgs e)
        {
            //numbers[0] = 12;
            //numbers[1] = 10;
            //numbers[2] = 25;
            //numbers[3] = 10;
            //numbers[4] = 15;
            //numbersList.Add(23);
            //numbersList.Add(32);
            //numbersList.Add(35);
        }
        //Array Print
        private void button1_Click(object sender, EventArgs e)
        {
            displayArrays.Text = listText.Text;
        }
        //List Print
        private void button2_Click(object sender, EventArgs e)
         {
            displayStringArrays = text.Split('\n').ToArray();
            foreach (var item in displayStringArrays)
            {
                displayArraysString.Text += item;

                 if (listText.Text == item.Substring(0, item.Length - 1) || listText.Text == item)
                {
                     MessageBox.Show("Found a match!");
                }
                else
                {
                    //Something.
                }
            }

         }
    }
}

replace this code with your code. I checked this it is working fine now.

Upvotes: 1

Related Questions