Home > Back-end >  C# parsing information from a text file
C# parsing information from a text file

Time:08-12

I am trying to parse some data from a folder that contains text files. I have gotten to the point where I have obtained the desired data in a string array. I would like to parse the data as follows [int, int, string, double] currently, it looks like [string, string, string, string].

Now I know I cannot save the data into the same array but I was hoping to parse the integers and use them for coordinates, then parse the string and use it as a name, and finally parse the double as a theta.

Right now this is what I have

foreach (var str in data) 
{
   // so this will look into every string[] that is within d,ata. However I am running a 
   //blank on how to parse the data as desired.
}

If anyone has suggestions that would be great, most of my google searches have led to more confusion.

EDIT:

so the data would be stored in a text file and look something like this

12 45 acute .67
32 78 hypotenuse .12
.
.
.
and so on.

I used a Tuples list to achieve this but I am getting a weird result where all of my ints are stay the same as the first line of ints

so instead of the list looking like

(12 45 acute .67)
(32 78 hypotenuse .12)
.
.
.
and so on

it looks like

(12 45 acute .67)
(12 45 hypotenuse .12)
.
.
.
and so on

below is my current code

so I initialized a Tuple and another list to hold the integers

then in my for loop

    foreach(var line in File.ReadLines(file))
    {
         var values = line.Split(" ");
         for (var i = 0; i < 2; i  )
         {
               //this is the integer list
               positions.Add(int.Parse(values[i]));
         }
         var category = values[3];
         var certainty = Double.Parse(values[4]);
         // now we add all this data to the Tuple list
         results.Add(new(positions[0], positions[1],
                     category, certainty));
     }

So as I wrote out that code, I realized that I am hard coding my results to display only the first pair of x,y coordinates instead. Correct me if I am wrong. So the change should look something like

results.Add(new(positions.Item1, positions.Item2,
                cateogry, certainty));

CodePudding user response:

You can create a class.

class Coordinate {
  int X;
  int Y;
  string Name;
  double Theta;
}

Later, create a List from this dummy class.

List<Coordinate> myList = new List<Coordinate>();

As I understand that this data is ordered. Iterate your incoming data ,create an object and fill the list like this.

for(int i = 0; i < data.Length; i  )
{
   Coordinate myCoord = new Coordinate();
   var parsingResult;
   int modResult = i%4;
   switch(modResult) {
    case 0:  int.Parse(data[i], parsingResult); myCoord.X = parsingResult;  break; // Integer
    case 1:  int.Parse(data[i], parsingResult); myCoord.Y = parsingResult; break; // Integer
    case 2:  myCoord.Name = data[i]; break; // String
    case 3:  double.Parse(data[i], parsingResult); myCoord.Theta = parsingResult; break; // Double
   }
   myList.Add(myCoord);
}
  • Related