I have made an exercise where I need to calculate the average per row of a .txt file. (see below)
Toa Narumi gradeA 10 8 7 4 6,5
Jean-François Le Clerk gradeB 5 4 7
Joe GradeC 10 10
I only want the numbers behind the name and grade. This is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Linq;
namespace RenoD_Oef_Strings
{
class Program
{
static void Main(string[] args)
{
// Variablen
StreamReader bestand = new StreamReader(@"F:\RenoD_Oef_Strings\Punten.txt");
string strRow; // String used to read every row of .txt file
double?[] dblPoints; // Double array used to store all values
List<string> strInput = new List<string>(); // String List used to store all data from .txt
// Code
while ((strRow = bestand.ReadLine()) != null) // Read row per row in .txt
{
strInput = strRow.Split(' ').ToList(); // Puts every word in the row in to the list
int intX = 0;
foreach(string strX in strInput) // Calculate how many strings are in the list
{
intX++;
}
dblPoints = new double?[intX]; // Calculate the max number of elements the double array can have
intX = 0;
foreach(var x in strInput) // Checks if elements in the list can be converted in to a double
{
try
{
double dblR = Convert.ToDouble(x); // If element can be converted in to a double, it will be stored in the double array
dblPoints[intX] = dblR;
intX++;
}
catch
{
intX++; // If element CAN NOT be converted in to a double, it will be still be stored in the double array but without any value
}
}
double dblAverage = 0; // Double used to save the total average of one row
intX = 0;
foreach(var k in dblPoints)
{
if (k.HasValue) // Elements without value will be ignored
{
dblAverage += Convert.ToDouble(k); // All double values will be added up
intX++; // Used to see how much double values there are to calculate average
}
}
dblAverage = Math.Round(dblAverage/intX, 2); // Calculate average + round up to two decimals
Console.WriteLine(dblAverage.ToString());
}
bestand.Close();
Console.ReadKey();
}
}
}
What I have done is
Read row per row .txt file
Put all elements of that row in a list
Calculate how many elements that the double array will need to store (same number of elements the list has), which I later on use to store all doubles of the list
Check if elements in the list can be converted in to a double array; If the element can be converted in to a double, it will be stored in the double array.
If element CAN NOT be converted in to a double, it will be still be stored in the double array but without any value.
- Only calculate the average of the elements in my double array that have values.
I have tested the code, and it works without any issue.
My question is, have I taken a good approach? What would you have done to make it more efficient?
I have tried searching in other threads for solutions. I have seen simular questions, but was unable to find any exactly the same as my exercise.