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? Commented May 23, 2017 at 23:59
  • Possible duplicate of Concatenate integers in C# Commented 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. Commented 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
Sign up to request clarification or add additional context in comments.

2 Comments

don't you mean data[0] and data[1] ?
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.
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 Comment

you can also use int.TryParse if you are not sure of values passed

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.