1

I am trying to store a user input in a list. The user can enter numbers separated by a comma and then I need to perform some action based on the input that's why i am using IF later on and adding the input in a list. However if i try this then i cannot convert []string to int .
I tried to use inputs.Add(Convert.ToInt32(separatedInput)); but i get that i am Unable to cast object of type 'System.String[]' to type 'System.IConvertible'.'

List<int> userDecision = new List<int>();
            Console.WriteLine("enter number separated by comma ");
            var userInputs = Console.ReadLine();
            var separatedInput = userInputs.Split(',');
            var inputs = new List<int>();
            inputs.Add(separatedInput);



            if (input == 1)
            {
                userDecision.Add(1);
            }
            if (input == 2)
            {
                userDecision.Add(2);
            }
            if (input == 3)
            {
                userDecision.Add(3);
            }
            if (input == 4)
            {
                userDecision.Add(4);
            }
            if (input == -1)
            {
                userDecision.Add(-1);
            }
Bernard Vander Beken
  • 4,848
  • 5
  • 54
  • 76
  • That would need to parse the values to `int` `inputs.AddRange(separatedInput.Select(int.Parse))` though it will throw if any of the values cannot be parsed to `int`. – juharr May 12 '20 at 14:24
  • You're trying to add an array of strings (`separatedList`) to a list of integers, which doesn't make sense! – Sean May 12 '20 at 14:24
  • if you want to turn text in the form of e.g. `"123"` to a number, or in other words _parse_ it, use `int.Parse(...)` – Felk May 12 '20 at 14:25
  • @Sean well i know that, what is the other way to get more user values in a list ? – Vanessa Kensington May 12 '20 at 14:28
  • @VanessaKensington - if you know that then don't do it! – Sean May 12 '20 at 14:30
  • Does this answer your question? [C#: Is there a LINQ way to create an array of objects given an array of constructor parameters?](https://stackoverflow.com/questions/2113115/c-is-there-a-linq-way-to-create-an-array-of-objects-given-an-array-of-construc) – Ajith May 12 '20 at 14:45

3 Answers3

2

You can make your list of int by projecting the split string[], using int.Parse and then ToList

var inputs = userInputs.Split(',').Select(int.Parse).ToList();

Note that all the values MUST be parseable to an int, or the above line will fail.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
1

You need only below line of code.

List<int> userDecision= Array.ConvertAll(Console.ReadLine().Trim().Split(','),int.Parse).ToList<int>();
Ajith
  • 1,447
  • 2
  • 17
  • 31
1

You can try using Linq and query userInput into desired List<int> userDecision (let's get rid of temporary separatedInput, input and especially of batch of ifs):

using System.Linq;

...         

Console.WriteLine("enter number separated by comma ");

List<int> userDecision = Console
  .ReadLine()
  .Split(',')
  .Select(item => (success : int.TryParse(item, out int value), 
                     input : value))
  .Where(record => record.success && 
                  (record.input == -1 || record.input >= 1 && record.input <= 4))
  .Select(record => record.input)
  .ToList();     
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215