0

Hi Im currently a student trying to figure out how to search a 2D Array in C#. The program that Im trying to make is an Address Book. Basically I want to search the array for a keyword such as "First Name", I then want to be able to print the result of that search and be able to edit that part of the array. Im just wondering if anyone knows a particular syntax such as Array.Find("First Name"). Heres the code, and thanks for looking and helping me. :)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication9
{
    class AddressBook
    {
        static void Main(string[] args)
        {
            string InputSelection;
            char Selection_Char;

            string[,] AddressBook = new string[10, 2]//this is the array
    {
        {"First Name", "Last Name"},//i want to be able to search this array for First Name
	    {"XXXXX", "XXXXX"},
	    {"XXXXX", "XXXXX"},
	    {"XXXXX", "XXXXX"},
	    {"XXXXX", "XXXXX"},
        {"XXXXX", "XXXXX"},
	    {"XXXXX", "XXXXX"},
	    {"XXXXX", "XXXXX"},
	    {"XXXXX", "XXXXX"},
	    {"XXXXX", "XXXXX"}
	};
        Start:
            Console.WriteLine("==========Address Book==========");
            Console.WriteLine("Press 1 to Add an Entry.");
            Console.WriteLine("Press 2 to Delete an Entry.");
            Console.WriteLine("Press 3 to Print Book to Screen.");
            InputSelection = Console.ReadLine();
            Selection_Char = Convert.ToChar(InputSelection);// Converts the Users input to char
2
Contributors
10
Replies
15
Views
5 Years
Discussion Span
Last Post by Antenka
Featured Replies
  • Actually never mind, Ive debugged the code. Thankyou very much for you help Antenka. I will give you rep and Ill make this thread answered when Im finished. thanks again Read More

  • 1

    The scope of visibility of a [iCODE]j[/iCODE] is between [iCODE]{}[/iCODE] of you cycle. Because it was declared in the for-cycle. You have 2 ways to go: 1. Move declaration to a higher level to extend the boundaries, where this variable can be visible. 2. Save the value of j variable … Read More

0

Yes its a requirement, I was thinking of using a collection but I have to use an Array, Thanks for the quick reply though

0

Thank you Antenka, but I've actually managed to figure out how to search the whole array.My only problem is now that I cant print out the location of the result in the array like say AddressBook[0,1] so that my program can just immediately edit that row and column of the array. But heres the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication9
{
    class AddressBook
    {
        static void Main(string[] args)
        {
            string InputSelection;
            char Selection_Char;

            string[,] AddressBook = new string[10, 2]//this is the array
    {
        {"First Name", "Last Name"},//i want to be able to search this array for First Name
	    {"XXXXX", "XXXXX"},
	    {"XXXXX", "XXXXX"},
	    {"XXXXX", "XXXXX"},
	    {"XXXXX", "XXXXX"},
        {"XXXXX", "XXXXX"},
	    {"XXXXX", "XXXXX"},
	    {"XXXXX", "XXXXX"},
	    {"XXXXX", "XXXXX"},
	    {"XXXXX", "XXXXX"}
	};
        Start:
            Console.WriteLine("==========Address Book==========");
            Console.WriteLine("Press 1 to Add an Entry.");
            Console.WriteLine("Press 2 to Delete an Entry.");
            Console.WriteLine("Press 3 to Print Book to Screen.");
            InputSelection = Console.ReadLine();
            Selection_Char = Convert.ToChar(InputSelection);// Converts the Users input to char


            if (Selection_Char == '1')
            {
                Console.WriteLine("First Name: " + AddressBook[0, 0] + ". Last Name: " + AddressBook[0, 1] + ".");
                Console.WriteLine("First Name: " + AddressBook[1, 0] + ". Last Name: " + AddressBook[1, 1] + ".");
                Console.WriteLine("First Name: " + AddressBook[2, 0] + ". Last Name: " + AddressBook[2, 1] + ".");
                Console.WriteLine("Please enter the First Name for Contact 1.");
                AddressBook[0, 0] = Console.ReadLine();
                Console.WriteLine("Please enter the Last Name for Contact 1.");
                AddressBook[0, 1] = Console.ReadLine();
                Console.WriteLine("Address Book has now been updated.");
                Console.WriteLine("First Name: " + AddressBook[0, 0] + ", Last Name: " + AddressBook[0, 1] + ".");

                for (int i = 0; i < 10; i++)
                { //10 is the number of rows in the 2D table-like array
                    for (int j = 0; j < 2; j++)
                    { //2 is the number of columns in the 2D table-like array
                        string SearchInput = Console.ReadLine();
                        if (AddressBook[i, j].Equals(SearchInput, StringComparison.OrdinalIgnoreCase))
                        {
                           
                            Console.WriteLine("they match");
                            Console.ReadLine();
                            goto Start;
                        }
                        else
                        {
                            Console.WriteLine("Sorry they dont match.");
                            goto Start;
                        }

                    }


                }
0

I was thinking the same thing lol, but I dont have a clue how Im going to code it:-/

0

Uhm .. owkey .. let's try to compose it. For example, you found a match in the element AddressBook[0,1], so you need to write the exact "AddressBook[0,1]".

Let's divide this string into 2 parts:
1. Static. Means that it would remain the same for each output: "AddressBook[", "," and "]".
2. Dynamic. Means that you have to obtain it's value each time: "i" and "j"

To concatenate your strings you use "+" sign.
The most interesting part is that you already have such combined output in your code, e.g. here:

Console.WriteLine("First Name: " + AddressBook[0, 0] + ". Last Name: " + AddressBook[0, 1] + ".");

Now, try to compose ... ;)

0

:P Ohhhh now I get it, Thanks.
But another problem came out of nowhere >.<
Im getting an error where the j is, saying its unreachable code?

if (Selection_Char == '4')

                    for (int i = 0; i <= 10; i++)
                    { //10 is the number of rows in the 2D table-like array
                        for (int j = 0; j <= 2; j++)//This j saying its unreachable code
                        { //2 is the number of columns in the 2D table-like array
                            Console.WriteLine("Type in what you wanna search:");
                            string SearchInput = Console.ReadLine();
                            if (AddressBook[i, j].Equals(SearchInput, StringComparison.OrdinalIgnoreCase))
                            {
                                Console.WriteLine("Match - " + AddressBook[i, j] + ".");
                                Console.WriteLine("they match");
                                Console.ReadLine();
                                goto Start;
                            }
                            else
                            {
                                Console.WriteLine("Sorry they dont match.");
                                goto Start;
                            }

                        }



                        //
                        //     {
                        //         string[] strArray = { "Trueman", "XXXXXX" };
                        //         string findThisString = Console.ReadLine();
                        //         int strNumber;
                        //         int strIndex = 0;
                        //         for (strNumber = 0; strNumber < strArray.Length; strNumber++)
                        //        {
                        //            strIndex = strArray[strNumber].IndexOf(findThisString);
                        //if (strIndex >= 0)
                        //                break;
                        //        }
                        //       Console.WriteLine("There is a match.",
                        //          strNumber, strIndex);
                        //

                        //     Console.ReadLine();
                        //       goto Start;
                    }

            }
        }
    }
}
1

Actually never mind, Ive debugged the code. Thankyou very much for you help Antenka. I will give you rep and Ill make this thread answered when Im finished. thanks again

Votes + Comments
Happy coding! ;)
1

The scope of visibility of a j is between {} of you cycle. Because it was declared in the for-cycle.

You have 2 ways to go:
1. Move declaration to a higher level to extend the boundaries, where this variable can be visible.
2. Save the value of j variable in a different variable with a larger scope. And then work with it.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.