Search code examples
c#arraysloopsconsole.readline

Using Console.ReadLine().Split() to fill a string array in a loop


I have the following very basic code:

static void Main(string[] args)
{
    int n = Convert.ToInt32(Console.ReadLine());

    for (int i = 0; i < n*3; i++)
    {
        string[] numbers = Console.ReadLine().Split();
        Console.WriteLine();
        Console.WriteLine(numbers[i]);
    }
}

It's supposed to take the following data:

3  
11 9 1  
14 90 232  
111 15 111  

It is taking the first number to determine the number of lines of data their are (there is a reason for this but outside of the scope of this question.
The loop should take line 2, 3 and 4 and populate the numbers array, splitting the data up so numbers[0] = 11, numbers[1] = 9, numbers[2] = 1... and so on. What I'm seeing is that it's putting the first number in the line into the array and moving on. Here is a preview of what it's doing currently:

3  
11 9 1

11  
14 90 232

90  
111 15 111

I was hoping the output would be:

3  
11 9 1

11 9 1  
14 90 232

14 90 232  
111 15 111

111 15 111

I'm probably doing something completely stupid and blatantly obvious but I'm still trying to learn C#.


Solution

  • Let's go step by step :

    1. You enter 3 so n = 3 and the code in the for loop will run 3 * 3 = 9 times.
    2. You enter 11 9 1 so string[] numbers = { "11", "9", "1" };.
    3. Then it will print a blank line.
    4. Since i = 0 now, it will print numbers[0] which is "11"(It will print 11).

    The output is the following at this time:

    3
    11 9 1
    
    11
    
    1. You enter 14 90 232, so string numbers = { "14", "90", "232" };.
    2. Blank line
    3. Now it's the second time we're going through the loop and i = 1. So it will print numbers[1] which is "90".

    The output is the following at this time:

    3
    11 9 1
    
    11
    14 90 232
    
    90
    
    1. You enter 111 15 111, so string numbers = { "111", "15", "111" };.
    2. Blank line
    3. Now it's the third time we're going through the loop and i = 2. So it will print numbers[2] which is "111".

    The output is the following at this time:

    3
    11 9 1
    
    11
    14 90 232
    
    90
    111 15 111
    
    111
    

    You will encounter an error if you enter something like 1 2 3 which contains three numbers, because it will be the forth time we will be going through the loop and i = 3 and since numbers contains three elements you will see the following:

    System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
    

    enter image description here

    I recommend string.Join() to get your expected result.

    Here's the solutions:

    static void Main(string[] args)
    {
        int n = Convert.ToInt32(Console.ReadLine());
    
        for (int i = 0; i < n*3; i++)
        {
            string[] numbers = Console.ReadLine().Split();
            Console.WriteLine();
            Console.WriteLine(string.Join(" ", numbers));
        }
    }