Frage

I have some code here, that should open a text file and parse it.

It is parsed by Tabs, and linebreaks

As far as i can see, it should store the parsed data in a 2 dimensional array.

array[line,data]

so

           System.IO.FileInfo enemyFile = new System.IO.FileInfo("Data\\enemies.txt"); 
        System.IO.StreamReader enemies = enemyFile.OpenText();
        string line;
        string[,] enemyInfo = new string[20,20]; // Array to store parsed text

        while ((line = enemies.ReadLine()) != null)
        {
            string[] items = line.Split('\n');
            string[] newItems;
            for (int i = 0; i < items.Length; i++)
            {
                Console.WriteLine(i);
                newItems = items[i].Split('\t');
                for (int i2 = 0; i2 < newItems.Length; i2++)
                {
                    enemyInfo[i, i2] = newItems[i2];
                    //testArray.ListArray(newItems);
                    Console.WriteLine("[{0},{1}] = {2}", i, i2, enemyInfo[i, i2]);
                    Console.ReadLine();

                }
                Console.WriteLine("-");

            }

should put the first parsed data from the first line into enemyInfo[0,0] and the next parsed data from the first line into enemyInfo[0,1] and so on.

At a linebreak, it should start storing the data in enemyInfo[1,0] and then enemyInfo[1,1] and so on.

  1. Enemies.txt

    Name of Race    Race_ID Class_ID    Sex_ID  ToHit   Evade   Damage  Strength    Dexterity   Constitution    Intelligence    Charisma    Wisdom  Experience  Level
    Goblin  0   0   2   0   1   -1  6   8   6   4   4   4   1   1
    Kobold  1   0   2   1   1   0   8   8   8   6   4   4   3   2
    

Is it just me that have done something wrong? no matter what i try, it never increments i in the first for loop, so it keeps storing the new lines in the same dimension of the array.

Hope i have supplied enough information.

Thanks in advance.

//Ronnie Henriksen

Edit:

Forgot to add example of the output i get.

[0,0] = Name of race
[0,1] = Race
and so on up to [0,14] and then it does this:
[0,0] = Goblin
[0,1] = 0
and so on, up to [0,14] and then it does the same with the next line ( kobold ).
War es hilfreich?

Lösung

Your error is in the splitting of the line read by ReadLine with \n, you should split directly this line with \t

   int i = 0;
   while ((line = enemies.ReadLine()) != null)
   {
        string[] items = line.Split('\t');
        for (int i2 = 0; i2 < items.Length; i2++)
        {
            Console.WriteLine(i2);
            enemyInfo[i, i2] = items[i2];
            Console.WriteLine("[{0},{1}] = {2}", i, i2, enemyInfo[i, i2]);
            Console.ReadLine();
        }
        i++;
   }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top