0

First, I initialize an ArrayList to my code then add each input by the user. I manage to add a data and show but after doing so it throws an exception of OutOfRange and I don't see any reason why.

I already asked about something like this but it was to check the array has value stored yet Link to my first question. But the problem I encountered now is something this exception

static ArrayList fName = new ArrayList();
static ArrayList cNumber = new ArrayList();

public static void createContact()
{
    Console.Write("\nEnter your full name: ");
    fullName = Console.ReadLine();
    fName.Add(fullName);

    Console.Write("\nEnter your contact number: ");
    contactNumber = Console.ReadLine();
    cNumber.Add(contactNumber);
}

public static void displayContact()
{
    if (fName.Count == 0 && cNumber.Count == 0)
    {
        Console.WriteLine("No Data Yet!");
    }

    else if (fName.Count > 0 && cNumber.Count > 0)
    {
        for (int j = 0; j < numbers.Length; j++)
        {
            Console.WriteLine(numbers[j] + ". " + fName[j] + " - " + cNumber[j]);
        }
    }

    else if (fName.Count <= 10 && cNumber.Count <= 10)
    {
        Console.WriteLine("Maximum data acquired! Terminating program...");
    }
}

The output goes something like this:

Output 1

Output 2

uvr
  • 515
  • 4
  • 12
Testtest
  • 23
  • 5
  • I'm not sure what `numbers` represents, but evidently it has more items than `fName` or `cNumber`. I recommend learning to [debug](https://learn.microsoft.com/en-us/visualstudio/get-started/csharp/tutorial-debugger). You can step through your code with the debugger and inspect individual variables to see what data they hold, etc. – ProgrammingLlama Apr 28 '21 at 09:52
  • numbers represents as the numbered list for my arrays that being input by the user – Testtest Apr 28 '21 at 09:54
  • Does `numbers` contain more items than `fName` or `cNumber`? – ProgrammingLlama Apr 28 '21 at 09:55
  • Sorry, I am really new in doing arraylist. I've no idea on what I'm doing, I just do what my mind thinks is right but I got it all wrong on that exception. – Testtest Apr 28 '21 at 09:56
  • OK, imagine `numbers` contains 2 items, so your loop (`for (int j = 0; j < numbers.Length; j++)`) visits item at index 0 first, and then item at index 1 next. `fName`, on the other hand, only contains 1 item. So on the first loop, you are able to access the item at index 0. On the second loop, you ask for the item at index 1 but that doesn't exist. That's why you get this error. – ProgrammingLlama Apr 28 '21 at 09:57
  • I think my array `numbers` contains more items than my two `arraylist` since I take inputs from the user, once the user done adding, the menu will pop again then ask the user which option he/she wants, if he choose 2 he will just show what was the user added. – Testtest Apr 28 '21 at 09:58
  • oh, I think I already found a hint. I'll try it right away. Anyway, thanks @Llama – Testtest Apr 28 '21 at 09:59
  • @Llama I tried using my arraylist size and it works. `(for int j = 0; j < fName.Count; j++) and now I don't have any exception. Thanks for the idea once again – Testtest Apr 28 '21 at 10:16

0 Answers0