String array to Int array
arrays, c#
Solution
Here you go:
class Program
{
static void Main()
{
string numberStr = Console.ReadLine(); // "1 2 3 1 2 3 1 2 ...."
string[] splitted = numberStr.Split(' ');
int[] nums = new int[splitted.Length];
for(int i = 0 ; i < splitted.Length ; i++)
{
nums[i] = int.Parse(splitted[i]);
}
}
}
Problem
I am trying to get a string from the console and put all elements in an int array. It throws an error that my input was in a wrong format. I am trying with "1 1 3 1 2 2 0 0" and I need those as int values and later perform some calculations with them. Here is my attempt: ``` class Program { static void Main() { string first = Console.ReadLine(); string[] First = new string[first.Length]; for (int i = 0; i < first.Length; i++) { First[i] += first[i]; } int[] Arr = new int[First.Length];//int array for string console values for (int i = 0; i < First.Length; i++)//goes true all elements and converts them into Int32 { Arr[i] = Convert.ToInt32(First[i].ToString()); } for (int i = 0; i < Arr.Length; i++)//print array to see what happened { Console.WriteLine(Arr[i]); } } } ```