Prajakta
Prajakta

Reputation: 31

How to read multiple input lines in C# v4.2.6 console?

I want to read multiple input lines in C# v2.4.6 I have entered 3 input lines like below

4
2 5 6 3
20 40 90 50

I use string line=Console.ReadLine()

The first time it reads 4 then I use:

string abc=Console.ReadLine();
string xyz=Console.ReadLine();

but the output shows abc[0]=2 and xyz[0]=2

Please suggest any solution

Upvotes: 0

Views: 6797

Answers (3)

Ganesh Sutar
Ganesh Sutar

Reputation: 1

Use Following Code for multiple inputs:

Use spaces for multiple inputs:

    Console.WriteLine("Enter Tokens : ");

    string Token = Console.ReadLine();
    string[] T = Token.Split(' ');

    Console.WriteLine("Tokens : ");
    for (int i = 0; i < T.Length; i++)
    {

        Console.WriteLine(T[i]);

    }

Upvotes: 0

Prajakta
Prajakta

Reputation: 31

I have used the below code suggested by Pankaj Rawat and it's working.

   string line=Console.ReadLine()
   string[] abc = line.Split(' ');
   int y=Convert.ToInt32(abc[0]);

Upvotes: 2

Pankaj Rawat
Pankaj Rawat

Reputation: 4573

Console application ReadLine method won't read data from multiple lines. you can read data like this

Console.WriteLine("Enter your first name.");
        string firstName = Console.ReadLine();
        Console.WriteLine("Enter your last name.");
        string lastName = Console.ReadLine();
        Console.WriteLine("Enter your job title.");
        string jobTitle = Console.ReadLine();

Or

Console.WriteLine("Enter your data");
            string firstline = Console.ReadLine();
            string secondline = Console.ReadLine();
            string thirdline = Console.ReadLine();

            string input = string.Concat(firstline, secondline, thirdline);

Upvotes: 0

Related Questions