Hi
Ive currently got a problem with my code, its meant to step through the array and search the array. For some reason it only does the 1st line of array, it wont go through the rest of the array. The problem with that line of code is that it only searches the 1st line of the Array, and doesn't recognise when I search for all the other rows of the Array, theres only 10 rows in the array.

This is the code:
> {
>                Console.WriteLine("Type in the First Name of the person you want to search:");
>                string SearchInput5 = Console.ReadLine();
> 
>                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
>                    {
>                        if (AddressBook[i, j].Equals(SearchInput5, StringComparison.OrdinalIgnoreCase))// this line shows the error when I search for the 2n d line of the Array
>                     }
>                 }
> }
>

> I cant figure this out because it should be stepping through the whole array, it keeps coming up with an error when I search for another row of the array. This is the error:
> -IndexOutOfRangeException was unhandled.
> -Index was outside the bounds of the array.

Thanks for looking and helping me in advance.

for (int i = 0; i < AddressBook.GetLength(0); i++)   
{
       for (int j = 0; j < AddressBook.GetLength(1); j++)
       {
            if (AddressBook[i, j].Equals(SearchInput , StringComparison.OrdinalIgnoreCase ) )
       }
}

The <= makes the inner loop go around one extra time.

Thines01 Thanks for the reply but thats the not the problem, I took it the = out, but error keeps coming up.

Mazzica thanks for the reply, but Its still not working for some reason. Heres the code, I really cant understand why this isnot working :-/

class AddressBook
    {
        static void Main(string[] args)
        {
            string InputSelection, s, a;
            char Selection_Char;

            string[,] AddressBook = new string[10, 2]//this is the array
    {
        {"£££££", "£££££"},//Array
	    {"£££££", "£££££"},
	    {"£££££", "£££££"},
	    {"£££££", "£££££"},
	    {"£££££", "£££££"},
        {"£££££", "£££££"},
	    {"£££££", "£££££"},
	    {"£££££", "£££££"},
	    {"£££££", "£££££"},
	    {"£££££", "£££££"}
	};
if (Selection_Char == '6')
            {
                for (int q = 0; q < AddressBook.GetLength(0); q++)
                {
                    for (int e = 0; e < AddressBook.GetLength(1); e++)
                    {
                        string SearchingInput = Console.ReadLine();
                        if (AddressBook[q, e].Equals(SearchingInput, StringComparison.OrdinalIgnoreCase))
                        {
                            Console.WriteLine("MAtch" + AddressBook[q, e] + ".");
                            Console.ReadLine();
                            goto Start;
                        }
                    }
                }
            }

i had tried the code but has no error can you put the whole code

It looks like it working because all the rows of the array are the same. Ok heres the code for most of the program.

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

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

            string[,] AddressBook = new string[10, 2]//this is the array
    {
        {"£££££", "£££££"},//Array
	    {"£££££", "£££££"},
	    {"£££££", "£££££"},
	    {"£££££", "£££££"},
	    {"£££££", "£££££"},
        {"£££££", "£££££"},
	    {"Test", "Error"},
	    {"£££££", "£££££"},
	    {"£££££", "£££££"},
	    {"£££££", "£££££"}
	};
        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.");
            Console.WriteLine("Press 4 to Edit a Contact.");
            Console.WriteLine("Press 5 to Search the Address Book.");
            InputSelection = Console.ReadLine();
            Selection_Char = Convert.ToChar(InputSelection);// Converts the Users input to char


            if (Selection_Char == '1')
                for (int e = 0; e < AddressBook.Length / 2; e++)
                {
                    if ((AddressBook[e, 0] == "£££££"))
                    {
                        Console.WriteLine("Type in the First Name...");
                        AddressBook[e, 0] = Console.ReadLine();
                        Console.WriteLine("Type in the Last Name...");
                        AddressBook[e, 1] = Console.ReadLine();
                        Console.WriteLine("Contact has now been saved.");
                        Console.ReadLine();
                        goto Start;
                    }
                }


            if (Selection_Char == '2')
            {
                Console.WriteLine("Type in the First Name of the person you want to search:");
                string SearchInput5 = Console.ReadLine();

                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
                    {
                        if (AddressBook[i, j].Equals(SearchInput5, StringComparison.OrdinalIgnoreCase))
                        {
                            Console.WriteLine("Match - " + AddressBook[i, j] + ".");
                            int ioutput = Convert.ToInt32(i);
                            int joutput = Convert.ToInt32(j);
                            Console.WriteLine(+ioutput + " , " + joutput + ".");
                            Console.WriteLine("Match has been confirmed.");
                            Console.WriteLine("Are you sure you want to delete: " + AddressBook[ioutput, joutput] + " " + AddressBook[ioutput, joutput + 1] + ".");
                            InputSelection = Console.ReadLine();
                            Selection_Char = Convert.ToChar(InputSelection);
                            if (Selection_Char == 'y')
                            {
                                AddressBook[ioutput, joutput] = "£££££";
                                AddressBook[ioutput, joutput + 1] = "£££££";
                                Console.WriteLine("Contact has been successfully deleted.");
                                Console.ReadLine();
                                goto Start;
                            }
                            else
                            {
                                Console.WriteLine("Cancelled.");
                                goto Start;
                            }
                        }
                    }
                }
            }

            if (Selection_Char == '3')
            {
                for (int c = 0; c < AddressBook.Length / 2; c++)
                {
                    s = AddressBook[c, 0];
                    a = AddressBook[c, 1];
                    Console.WriteLine("{0}, {1}", s, a);
                    Console.ReadLine();
                }
                goto Start;
            }
            if (Selection_Char == '6')
            {
                for (int q = 0; q < AddressBook.GetLength(1); q++)
                {
                    for (int e = 0; e < AddressBook.GetLength(0); e++)
                    {
                        string SearchingInput = Console.ReadLine();
                        if (AddressBook[q, e].Equals(SearchingInput, StringComparison.OrdinalIgnoreCase))
                        {
                            Console.WriteLine("MAtch" + AddressBook[q, e] + ".");
                            Console.ReadLine();
                            goto Start;
                        }
                    }
                }
            }

you swaped the lengthes

for (int q = 0; q < AddressBook.GetLength(1); q++)
                {
                    for (int e = 0; e < AddressBook.GetLength(0); e++)
                    {

should be

for (int q = 0; q < AddressBook.GetLength(0); q++)
                {
                    for (int e = 0; e < AddressBook.GetLength(1); e++)
                    {

I switched the 0 and 1, its still not working >.< what does the (0) and (1) do?
By the way thanks for helping me

Another thing I just realised when I add i+1 it seraches and returns whats in the 2nd row of the array (which I think is weird), but when I search for the 3rd row of the array it comes up with the error again.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication9
{
    class AddressBook
    {
        static void Main(string[] args)
        {
            string InputSelection, s, a;
            char Selection_Char;
 
            string[,] AddressBook = new string[10, 2]//this is the array
    {
        {"£££££", "£££££"},//Array
	    {"£££££", "£££££"},
	    {"£££££", "£££££"},
	    {"£££££", "£££££"},
	    {"£££££", "£££££"},
        {"£££££", "£££££"},
	    {"Test", "Error"},
	    {"£££££", "£££££"},
	    {"£££££", "£££££"},
	    {"£££££", "£££££"}
	};
        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.");
            Console.WriteLine("Press 4 to Edit a Contact.");
            Console.WriteLine("Press 5 to Search the Address Book.");
            InputSelection = Console.ReadLine();
            Selection_Char = Convert.ToChar(InputSelection);// Converts the Users input to char
 
 
            if (Selection_Char == '1')
                for (int e = 0; e < AddressBook.Length / 2; e++)
                {
                    if ((AddressBook[e, 0] == "£££££"))
                    {
                        Console.WriteLine("Type in the First Name...");
                        AddressBook[e, 0] = Console.ReadLine();
                        Console.WriteLine("Type in the Last Name...");
                        AddressBook[e, 1] = Console.ReadLine();
                        Console.WriteLine("Contact has now been saved.");
                        Console.ReadLine();
                        goto Start;
                    }
                }
 
 
            if (Selection_Char == '2')
            {
                Console.WriteLine("Type in the First Name of the person you want to search:");
                string SearchInput5 = Console.ReadLine();
 
                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
                    {
                        if (AddressBook[i+1, j].Equals(SearchInput5, StringComparison.OrdinalIgnoreCase))//I added the +1 here
                        {
                            Console.WriteLine("Match - " + AddressBook[i+1, j] + ".");
                            int ioutput = Convert.ToInt32(i+1);
                            int joutput = Convert.ToInt32(j);
                            Console.WriteLine(+ioutput + " , " + joutput + ".");
                            Console.WriteLine("Match has been confirmed.");
                            Console.WriteLine("Are you sure you want to delete: " + AddressBook[ioutput, joutput] + " " + AddressBook[ioutput, joutput + 1] + ".");
                            InputSelection = Console.ReadLine();
                            Selection_Char = Convert.ToChar(InputSelection);
                            if (Selection_Char == 'y')
                            {
                                AddressBook[ioutput, joutput] = "£££££";
                                AddressBook[ioutput, joutput + 1] = "£££££";
                                Console.WriteLine("Contact has been successfully deleted.");
                                Console.ReadLine();
                                goto Start;
                            }
                            else
                            {
                                Console.WriteLine("Cancelled.");
                                goto Start;
                            }
                        }
                    }
                }
            }

if you are going to set it manual use -1

for (int i = 0; i <= 10; i++)
{                    
     for (int j = 0; j <= 2; j++)

should be

for (int i = 0; i <= 9; i++)
{                    
     for (int j = 0; j <= 1; j++)

The 0 and 1 in array.GetLength() indicate the dimension of the array to get the length of. When you had them switched, you were looking for the value of your AddressBook array at an index that doesn't exist. In an array like int[] arr = new array[2], you can access index 0 or index 1. Index 2 would be out of bounds, because it doesn't exist as defined. When you had the GetLength() number swapped, your program tried to do that.

Your most recent post (where you included i+1 in your loop) has two things going on. First, you are still using i <= 10 and j <= 2, which you can't do when accessing array elements. Arrays are zero-based, and so your loop searches for AddressBook[i, 0], AddressBook[i, 1], and then AddressBook[i, 2], which doesn't exist. That will give an out of bounds error.

Using i + 1 in the loop will always increase the row value by 1: on the first iteration, i = 0, and then when you access AddressBook you are accessing AddressBook[1, j]. That's why the row is increased from what you intended.

As for your error, are you sure that your program fails when accessing the third row, or might your program be trying to access the (non-existant) third column of a given row? The second will definitely happen if that part of the code tries to execute, and I don't see why it wouldn't if it's running as written in your post.

Ohh Woooow thankyou Mazzica, I didnt realise it was that simple, Thankyou very much :)!

Oh alright tanks Kay for the explanation

you are welcome have fun with coding

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.