-1

Good Morning! So I want to create a Multidimensional Array for a CSV-File in C#. I programmed this Code, but it doesn't work. Can somebody help me?

        StreamReader sr = new StreamReader(@"C:\_repos\2021-3xhit-p1-team-02\Programm\CodeZuweisen.txt");

      
        string[,] info = new string[10,6];
        int line = 0;

        string content = sr.ReadToEnd();
        string[] s = content.Split(',');

        for (int i = 0; i < s.Length; i++)
        {

            if (i % 6 == 0 && i!=0)
            {
                ++line;
            }
            else
            {
                info[line, i] = s[i];
            }
        }


        lblAusgabe.Text = info[1,2];

If I want to run this program, there is no output? I already programmed it in java, and it also works, but I don't like C# :D.

Any kind of help would be awesome! Have a nice day!

Benzara Tahar
  • 2,058
  • 1
  • 17
  • 21
ception
  • 33
  • 3
  • can you please show your data of excel... – MD. RAKIB HASAN Jan 19 '21 at 09:12
  • it's a text file – Benzara Tahar Jan 19 '21 at 09:12
  • 1
    Please post a [mcve] including your input and expected output, so we can replicate the problem. – Fildor Jan 19 '21 at 09:16
  • Btw: you are not resetting `i`. There cannot be a non-null string in [1,2]. I wonder why you are _not_ getting an IndexOutOfBoundsException ... maybe I am missing something, here. – Fildor Jan 19 '21 at 09:17
  • I think `info[line, i] = s[i];` should be `info[line, i%6] = s[i];` for starters. – Fildor Jan 19 '21 at 09:19
  • 1
    _"I already programmed it in java, and it also works"_ - I have my doubts. The Java code for this should be _pretty_ close to what this code is. So if you have the same (="equivalent") in Java, it _cannot_ "work". – Fildor Jan 19 '21 at 09:23
  • Without input/output and information about the error/issue you encounter. The mother dupe https://stackoverflow.com/questions/5282999/reading-csv-file-and-storing-values-into-an-array will be the way to go – Drag and Drop Jan 19 '21 at 09:36
  • @MD.RAKIBHASAN Thanks for the fast answer! This is my random created csv file. does,riding,addition,shop,wheat,got various,purple,your,universe,roar,must attention,throughout,tank,excellent,success,sunlight therefore,sat,seldom,typical,born,sheet done,prevent,announced,wooden,chest,higher airplane,running,below,knew,gift,broke magic,pretty,nearly,pitch,through,aware – ception Jan 19 '21 at 13:00
  • @Fildor Thanks for the fast answer! This is my CSV filedoes,riding,addition,shop,wheat,got various,purple,your,universe,roar,must attention,throughout,tank,excellent,success,sunlight therefore,sat,seldom,typical,born,sheet done,prevent,announced,wooden,chest,higher airplane,running,below,knew,gift,broke magic,pretty,nearly,pitch,through,aware – ception Jan 19 '21 at 13:01
  • @Fildor I want to put them in a multidimensional array called string info[column,row]. For example the word "throughout" in my CSV-File is saved in the string[1,1] – ception Jan 19 '21 at 13:04
  • @Fildor java: while(scanner.hasNext()){ String line = scanner.nextLine(); String[] s = line.split(" "); for (int m = 0; m < s.length ; m++) { d[i][m] = s[m]; System.out.println(s[m]); } i++; } – ception Jan 19 '21 at 13:07
  • ^^ See? Here you split by lines first, then by splitter. Quite a different enough approach for it to work but the C# version not. – Fildor Jan 19 '21 at 13:14
  • Addn information in the question using the [edit] button. Format does not survive in comment. – Drag and Drop Jan 19 '21 at 16:14

1 Answers1

3

First of all, string.Split(',') isn't a CSV parsing tool. You should use a dedicated CSV parser and a strongly typed output, eg. CSV Helper.

Secondly, ReadToEnd will give you one long string. The solve is to split by new line first. However, it's probably easier to use a jagged array here (which will produce a string[][]). Jagged arrays are a little easier to work with as you can use standard Linq methods that take an IEnumerable and you don't need to dig out the dimensions all the time.

Example

var myArray = File.ReadAllLines("FileName")
                  .Select(x => x.Split(','))
                  .ToArray();

// print the results to screen
foreach (var line in myArray)
   Console.WriteLine(string.Join(", ", line));
halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141