I am attempting to build an array of batting averages from a .txt file, but most of the resources I am finding online to assist in how to work this out either work with text in the following format:
1 2 3 4 5
2 3 4 5 6
etc.
... or in the following format:
1, 2
3, 4
etc.
Unfortunately, my text file is in the following format, and changing the format is not an option. Each odd line number denotes a player, with the following even line number being their average at bat in that instance (just information for context):
1
2
3
4
Using this file, I want to assign the odd numbers to the index of the array, and then populate the even numbers as values, calculating them as I go. I believe that I can parse out how to accomplish the calculations, I just cannot ascertain how to grab the data nor associate the odd numbers with array indexes.
Code as follows. File is to be specified by user:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
static string FileName()
{
string doc = Console.ReadLine();
return doc;
}
static void Main(string[] args)
{
try
{
Console.Write("Where is the data file for batting averages located? ");
string doc = FileName();
StreamReader reader = new StreamReader(doc);
string avg = reader.ReadToEnd();
reader.Close();
}
catch (System.IO.FileNotFoundException)
{
Console.WriteLine("The file can not be found.");
}
catch (System.FormatException)
{
Console.WriteLine("Invalid file.");
}
catch (System.ArgumentException)
{
Console.WriteLine("Enter a valid file name.");
}
catch (System.Exception exc)
{
Console.WriteLine(exc.Message);
}
}
Any assistance would be welcome. Please keep in mind I am a student and still learning the earliest parts of C#, so any truly advanced techniques are probably going to be lost on me.