printing 2d array in c# through for loop

前端 未结 1 616
不知归路
不知归路 2021-01-29 10:15

This is my filing function that takes data from the file and places it in the array:

public void populate_grid_by_file()
    {
        String store_data_from_fil         


        
相关标签:
1条回答
  • 2021-01-29 10:45

    you are splitting the data into lines, and it appears your intention in the inner loop is to process the characters in the current line. However, you are actually processing the entire file for each line iteration, which is why your output contains all the file's characters * the number of lines. Try this adjustment instead:

       public void populate_grid_by_file()
            {
                String store_data_from_file = string.Empty;
                try
                {
                    using (StreamReader reader = new StreamReader("data.txt"))
                    {
                        store_data_from_file = reader.ReadToEnd().ToString();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                string[] line = store_data_from_file.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < line.Length; i++)
                {
                    //string[] alphabet = store_data_from_file.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
                    string[] alphabet = line[i].Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries)
                    for (int j = 0; j < alphabet.Length; j++)
                    {
                        Sodoku_Gri[i, j] = alphabet[j];
                    }
                }            
            }
    
    0 讨论(0)
提交回复
热议问题