Home > Back-end >  Convert String[*,*] into int[*,*] in C# winform application
Convert String[*,*] into int[*,*] in C# winform application

Time:09-14

Hello everyone I am new to programming in c#, what I am going to do is that I want to convert the 2D string array to 2D integer array, the reason for this conversion is that I want to pass that integer array to another method for some calculation. Thanks in advance for helping.

        public void Matrix()
        {
            int a = int.Parse(txtRowA.Text);
            int b = int.Parse(txtColA.Text);


            Random rnd = new Random();
            int[,] matrixA = new int[a, b];

            string matrixString = "";
            
            for (int i = 0; i < a; i  )
            {
                for (int j = 0; j < b; j  )
                {
                    matrixA[i, j] = rnd.Next(1, 100);

                    matrixString  = matrixA[i, j];
                    matrixString  = "    ";

                   
                }
                matrixString  = Environment.NewLine;
            }

            

            txtA.Text = matrixString;
            txtA.TextAlign = HorizontalAlignment.Center;


        }

CodePudding user response:

Your code is actually pretty close. Try this:

private Random rnd = new Random();

public int[,] Matrix(int a, int b)
{
    int[,] matrixA = new int[a, b];
    for (int i = 0; i < a; i  )
    {
        for (int j = 0; j < b; j  )
        {
            matrixA[i, j] = rnd.Next(1, 100);
        }
    }
    return matrixA;
}

What you have there is a function that does not rely on any WinForms controls and will produce your int[,] quickly and efficiently.

Let the calling code work with the WinForms controls:

int a = int.Parse(txtRowA.Text);
int b = int.Parse(txtColA.Text);
int[,] matrix = Matrix(a, b);

Now you can pass the matrix to anything that requires an int[,].

If you need to display the matrix then create a separate function for that:

public string MatrixToString(int[,] matrix)
{
    StringBuilder sb = new();
    for (int i = 0; i < matrix.GetLength(0); i  )
    {
        string line = "";
        for (int j = 0; j < matrix.GetLength(1); j  )
        {
            line  = $"{matrix[i, j]}    ";
        }
        sb.AppendLine(line.Trim());
    }
    return sb.ToString();
}

Your calling code would look like this:

int a = int.Parse(txtRowA.Text);
int b = int.Parse(txtColA.Text);
int[,] matrix = Matrix(a, b);
UseTheMatrix(matrix);
txtA.Text = MatrixToString(matrix);
txtA.TextAlign = HorizontalAlignment.Center;

A sample run produces:

matrix

CodePudding user response:

You can use List as a helper to store the elements of the array extracted from the string, but first I replaced the SPACE between elements in your string with a special character '|' as a separator to make it easier to extract the numbers from the string.

you can do somthing like this:

public static int[,] ConvertToInt(string matrix)
    {
        List<List<int>> initial = new List<List<int>>();

        int lineNumber          = 0;
        int currenctIndex       = 0;
        int lastIndex           = 0;
        int digitCount          = 0;

        initial.Add(new List<int>());

        foreach (char myChar in matrix.ToCharArray())
        {

            if (myChar == '|')
            {
                
                initial[lineNumber].Add(int.Parse(matrix.Substring(lastIndex, digitCount)));
                lastIndex  = currenctIndex 1;
                digitCount = 0;
            }
            else
                digitCount  ;

            if (myChar == '\n')
            {
                lineNumber  ;
                if(currenctIndex < matrix.Length-1)
                    initial.Add(new List<int>());
            }

            currenctIndex  ;
            
        }

        int[,] myInt = new int[initial.Count, initial[0].Count];
        for (int i = 0; i < initial.Count; i  )
            for (int j = 0; j < initial[i].Count; j  )
                myInt[i, j] = initial[i][j];

        return myInt;
    }
  • Related