Home > Back-end >  Multidimensional array implementation to output Names and addresses in c#
Multidimensional array implementation to output Names and addresses in c#

Time:11-28

I have code like this, it requests input for name 1-5, and address 1-5, I want it to detect the name from the string, and in the search name section, it will request name from user input and outputs the address that was previously inputted.

string[,] input = new string[5, 2];
var Name = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

for (int a = 0; a < input.GetLength(0);   a)
{
    while (true)
    {
        Console.Write($"Name {a   1} : ");
        input[a, 0] = Console.ReadLine();

        if (Name.Add(input[a, 0]))
            break;

        Console.WriteLine("Name is already added! please input a different name");
    }

    Console.Write($"Address {a   1} : ");
    input[a, 1] = Console.ReadLine();
}

while(true)
{
    Console.Write("\n==> Search Name : ");
    string nameSearch = Console.ReadLine();

    if (Name.Contains(nameSearch)) 
    { 
        Console.Write($"Address : {input[1, 1]}"); 
    }
    else 
    { 
        Console.Write("Name cannot be found!");
    }
}

I already succeeded to make output like this

Name 1: example name a

Address 1: cannot be coded yet

Name 2: example name b

Address 2: cannot be coded yet

Name 3: example name c

Address 3: cannot be coded yet

Name 4: example name d

Address 4: cannot be coded yet

Name 5: example name e

Address 5: cannot be coded yet

And i want it to output:

Name 1: example name a

Address 1: address 1 input from user. ex: Mango number 7

Name 2: example name b

Address 2: address 2 input from user. ex: Pineapple number 12

Name 3: example name c

Address 3: address 3 input from user. ex: Blueberry number 44

Name 4: example name d

Address 4: address 4 input from user. ex: Apple number 3

Name 5: example name e

Address 5: address 1 input from user. ex: Cherry number 5

CodePudding user response:

A simple linear search will work.

while(true)
{
    Console.Write("\n==> Search Name : ");
    string nameSearch = Console.ReadLine();

    bool found = false;
    for(int i = 0; i < input.GetLength(0); i  )
    { 
        if (input[i, 0] == nameSearch) {
            Console.Write($"Address : {input[i, 1]}");
            found = true;
            break;
        }
    }
    if (!found)
    { 
        Console.Write("Name cannot be found!");
    }
}

You could check the hashmap before the loop, but you will still need to loop if it is found the hashmap contains the name.

while(true)
{
    Console.Write("\n==> Search Name : ");
    string nameSearch = Console.ReadLine();

    if (Name.Contains(nameSearch))
    {
        for(int i = 0; i < input.GetLength(0); i  )
        { 
            if (input[i, 0] == nameSearch) {
                Console.Write($"Address : {input[i, 1]}");
                break;
            }
        }
    }
    else
    { 
        Console.Write("Name cannot be found!");
    }
}

Or, you could get rid of the hashmap and the 2d array and use a Dictionary<string, string> where the key is the name and the value is the address.

var data = new Dictionary<string, string>();
for (int a = 0; a < 5;   a)
{
    string name;
    while (true)
    {
        Console.Write($"Name {a   1} : ");
        name = Console.ReadLine();
        if (!data.ContainsKey(name)) {
            break;
        }
        Console.WriteLine("Name is already added! please input a different name");
    }
    Console.Write($"Address {a   1} : ");
    var address = Console.ReadLine();
    data[name] = address;
}
while(true)
{
    Console.Write("\n==> Search Name : ");
    string nameSearch = Console.ReadLine();

    if (data.ContainsKey(nameSearch) )
    { 
        Console.Write($"Address : {data[nameSearch]}"); 
    }
    else 
    { 
        Console.Write("Name cannot be found!");
    }
}
  •  Tags:  
  • c#
  • Related