0

How can I input multiple integers or strings in one line?

For example, int p would take values:
1 2 3 4 5 6

but now I know the answer to my own question i.e:

var p= Console.ReadLine().split(' ');
  • 2
    It's not clear what you mean. Can you provide an example of what you've tried and how it isn't working? – David May 23 '17 at 23:59
  • Possible duplicate of [Concatenate integers in C#](https://stackoverflow.com/questions/1014292/concatenate-integers-in-c-sharp) – Malcolm Salvador May 24 '17 at 00:29
  • well, I now know how to input multiple values in one line so plz open the question again because it is now clear what I asked and I have myself given the answer. – Syed Hassan Farman Aug 30 '18 at 11:14

2 Answers2

2

You can read line with numbers or srings separated with spaces (or other symbols). Then you can split the line into parts and parse values.

var line = Console.ReadLine();
var data = line.Split(' ');
var i1 = int.Parse(data[0]); //first integer
var i2 = int.Parse(data[1]); //second integer
Backs
  • 24,430
  • 5
  • 58
  • 85
  • 5
    don't you mean `data[0]` and `data[1]` ? – NetMage May 24 '17 at 00:06
  • **From review queue**: May I request you to please add some context around your source-code. Code-only answers are difficult to understand. It will help the asker and future readers both if you can add more information in your post. – RBT May 24 '17 at 01:34
0

You will need to accept the single line and then split them up based on how you want the inputs separate. For example, if you make the users type in:

1,2,3

then you need to split around the comma and convert the input to numbers:

foreach (var sn in inp.Split(',')) {
    var n = Convert.ToInt32(sn);
    // work with n
}
NetMage
  • 26,163
  • 3
  • 34
  • 55