How to Search an Array and Display Results
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
Related Article: C# search line by line text
is a C# discussion thread by moone009 that has 9 replies, was last updated 2 years ago and has been tagged with the keywords: c#.
TrueCoding
Junior Poster in Training
65 posts since Apr 2010
Reputation Points: 17
Solved Threads: 1
Skill Endorsements: 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
TrueCoding
Junior Poster in Training
65 posts since Apr 2010
Reputation Points: 17
Solved Threads: 1
Skill Endorsements: 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;
}
}
}
TrueCoding
Junior Poster in Training
65 posts since Apr 2010
Reputation Points: 17
Solved Threads: 1
Skill Endorsements: 0
I was thinking the same thing lol, but I dont have a clue how Im going to code it:-/
TrueCoding
Junior Poster in Training
65 posts since Apr 2010
Reputation Points: 17
Solved Threads: 1
Skill Endorsements: 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;
}
}
}
}
}
TrueCoding
Junior Poster in Training
65 posts since Apr 2010
Reputation Points: 17
Solved Threads: 1
Skill Endorsements: 0
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
TrueCoding
Junior Poster in Training
65 posts since Apr 2010
Reputation Points: 17
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 1 Year Ago by
Antenka