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(' ');
3
  • 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, 2017 at 23:59
  • Possible duplicate of Concatenate integers in C# May 24, 2017 at 0: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. Aug 30, 2018 at 11:14

2 Answers 2

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
2
  • 5
    don't you mean data[0] and data[1] ?
    – NetMage
    May 24, 2017 at 0: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, 2017 at 1: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
}
1
  • 1
    you can also use int.TryParse if you are not sure of values passed May 24, 2017 at 0:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.