Converting a string into an integer array
I have input a string and I want to convert each element into an integer and store it individually in an array.For instance I input 123 as a string,i want to store it in array nums.The first index(nums[0]) should have 1 the second(nums[1])should have 2 and so on.Now here is what i`ve done(which is not working):
Code:
str=Console.ReadLine();
for(int 1=0;i<3;i++)
{
nums[i]=int.Parse(str[i]);
}
;
The error is that it cannot convert the first argument form a char to a string and ther are some invalid arguments.
I`m perplexed,anyone???
Re: Converting a string into an integer array
Code:
nums[i]=int.Parse(str.Substring(i,1));
Re: Converting a string into an integer array
Quote:
Originally Posted by ironman
I have input a string and I want to convert each element into an integer and store it individually in an array.For instance I input 123 as a string,i want to store it in array nums.The first index(nums[0]) should have 1 the second(nums[1])should have 2 and so on.Now here is what i`ve done(which is not working):
Code:
str=Console.ReadLine();
for(int 1=0;i<3;i++)
{
nums[i]=int.Parse(str[i]);
}
;
The error is that it cannot convert the first argument form a char to a string and ther are some invalid arguments.
I`m perplexed,anyone???
...1=0... is the culprit
I guess you wanted to write i=0. right ?