Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading two inputs from the user

I would like to get the user enters two different values using only ONE statement of Console.ReadLine(). More specifically, instead of using two different variables declarations with two different ReadLine() method - I want to let the user enters two variables at the same time using one ReadLine() method for further processing.

like image 923
Sinan Avatar asked Jan 20 '23 14:01

Sinan


2 Answers

Something like this?

var line = Console.ReadLine();
var arguments = line.Split(' ');
var arg1 = arguments[0];
var arg2 = arguments[1];

In this example, space delimits the arguments. You can use , or ; as a delimiter, if you would like to.

like image 57
Alex Aza Avatar answered Jan 22 '23 04:01

Alex Aza


Console.WriteLine("Enter values separated by space");
string input = Console.ReadLine();
string[] inputs = input.Split(' ');
foreach (string inp in inputs)
{
     Console.WriteLine(inp);  
}
like image 30
Bala R Avatar answered Jan 22 '23 02:01

Bala R