none
what is problem? RRS feed

  • Question

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    
    namespace ConsoleApplication12
    {
        class Program
        {
            static void Main(string[] args)
            {
                const string mis = @"C:\Users\Tsotne\Desktop\in.txt";
                FileStream fs = new FileStream(mis, FileMode.CreateNew);
                fs.Close();
    
                StreamWriter sw = new StreamWriter(mis, true);
                sw.WriteLine("6,73,6,71");
                sw.WriteLine("32,1,0,12");
                sw.WriteLine("3,11,1,134");
                sw.WriteLine("43,15,43,6");
                sw.WriteLine("55,0,4,12");
    
                sw.Close();
    
                FileStream fsO = new FileStream(mis, FileMode.Open);
                fsO.Close();
    
                StreamReader sr = new StreamReader(mis, true);
                string[] mas = new string[5];
                int[,] A = new int[5, 4];
    
                for (int i = 0; i < 5; i++)
                {
                    mas[i] = sr.ReadLine();
                }
                char[] charsToTrim = { ',' };
                for (int i = 0; i < 5; i++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        A[i, j] = Convert.ToInt32(mas[i].Split(charsToTrim));
                    }
                }
    
            }
              
     what is problem?
    Monday, December 16, 2013 7:47 PM

Answers

  • Hello GuruMan94,

    I run your code and found that it would throw the error:

    Unable to cast object of type 'System.String[]' to type 'System.IConvertible'.

    This is because mas[i].Split(charsToTrim) will return a string[] type, however, Convert.ToInt32 can only pass parameter which is object type. So you can have a try to change code below:

    A[i, j] = Convert.ToInt32(mas[i].Split(charsToTrim));

    To be:

    A[i, j] = Convert.ToInt32(mas[i].Split(charsToTrim)[j]);

    And as what Jo has said, please state your problem more clearly like what the error message is and which line code cause it so that we could undertstand what you want and we help you better.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 17, 2013 7:13 AM
    Moderator

All replies

  • Hi GuruMan,

    Welcomes you to MSDN forums..

    Pls understood every one time is very important. So while posting your queries,  explain brief about your scenario,  what is the problem, what you want, so that easily they can understood and you also get the solution ASAP. 

    So just copy paste some code and asking what is the problem? is not a way to get quick solution.

    So tell here what is your Problem?


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful" http://www.ijcstjournal.org/

    Tuesday, December 17, 2013 6:07 AM
  • Hello GuruMan94,

    I run your code and found that it would throw the error:

    Unable to cast object of type 'System.String[]' to type 'System.IConvertible'.

    This is because mas[i].Split(charsToTrim) will return a string[] type, however, Convert.ToInt32 can only pass parameter which is object type. So you can have a try to change code below:

    A[i, j] = Convert.ToInt32(mas[i].Split(charsToTrim));

    To be:

    A[i, j] = Convert.ToInt32(mas[i].Split(charsToTrim)[j]);

    And as what Jo has said, please state your problem more clearly like what the error message is and which line code cause it so that we could undertstand what you want and we help you better.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 17, 2013 7:13 AM
    Moderator