CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2005
    Posts
    31

    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???

  2. #2
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: Converting a string into an integer array

    Code:
     nums[i]=int.Parse(str.Substring(i,1));

  3. #3
    Join Date
    Jan 2003
    Location
    Bangalore, INDIA
    Posts
    180

    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 ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured