Reputation: 15
I have a file I have read into a C# console app that contains information such as this:
Player;Team;POS;HR;RBI;AVG
Abreu, J;CWS;1B;30;101;0.29
Altuve, J;HOU;2B;15;66;0.313
Andrus, E;TEX;SS;7;62;0.258
I have to now sort all 143 items on this list by the RBI column, descending. I'm fairly certain I have to go about this with a 2D array, since this class has not yet gotten into Lists or DataTables or anything of that sort.
Since the professor of this class likes us to use methods whenever possible, I thus far have this:
public static string[] openList()
{
string[] playerFile = File.ReadAllLines("players.txt");
return playerFile;
}
public static string[] splitList()
{
foreach (string playerInfo in openList())
{
string[] playerStuff = playerInfo.Split(';');
foreach (string player in playerStuff)
{
Console.Write(player + '\t');
//string individualPlayer = player + '\t';
}
Console.WriteLine();
}
}
I know printing it at this point is NOT what needs to be done, that will just give me a complete list, in the original order.
I suppose my question is this: Since I now have this list imported and split, how do I go about setting it up as a 2D array that I can then manipulate? I know how to work with them if I'm hardcoding them in. I guess I'm just missing something obvious here. A pointer in the right direction would be nice.
Thanks.
Upvotes: 0
Views: 55
Reputation: 34421
Try this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static List<Player> players = new List<Player>();
const string FILENAME = @"c:\temp\test.txt";
static void Main(string[] args)
{
StreamReader reader = new StreamReader(FILENAME);
string inputLine = "";
int lineCount = 0;
while ((inputLine = reader.ReadLine()) != null)
{
lineCount++;
inputLine = inputLine.Trim();
if (inputLine.Length > 0)
{
if (lineCount > 1)
{
string[] playerStuff = inputLine.Split(';');
players.Add(new Player(playerStuff));
}
}
}
reader.Close();
}
}
public class Player
{
public string name { get; set;}
public string team { get; set;}
public string position { get; set;}
public int hr { get; set;}
public int rbi { get; set;}
public double avg { get; set;}
public Player(string[] line)
{
this.name = line[0];
this.team = line[1];
this.position = line[2];
this.hr = int.Parse(line[3]);
this.rbi = int.Parse(line[4]);
this.avg = double.Parse(line[5]);
}
}
}
Upvotes: 0
Reputation: 27861
As a direct answer to your question, you can create the 2D array like this:
string[] entries = openList();
string[,] array_2d = new string[entries.Length, 6];
for(int i = 0 ; i < entries.Length ; i++)
{
string[] parts = entries[i].Split(';');
for (int j = 0; j < 6; j++)
{
array_2d[i, j] = parts[j];
}
}
//Use array_2d here
However, a better way to go about this is to create a class that represents an entry in the file. For example:
public class Entry
{
public string Player {get;set;}
public string Team {get;set;}
public string POS {get;set;}
public int HR {get;set;}
public int RBI {get;set;}
public double AVG {get;set;}
}
And then you can create a method (e.g. CreateEntry
) that takes in a line and parses it and returns an Entry
object.
The CreateEntry
method would look like something like this:
public Entry CreateEntry(string line)
{
string[] parts = line.Split();
return new Entry()
{
Player = parts[0],
Team = parts[1],
POS = parts[2],
HR = Convert.ToInt32(parts[3]),
RBI = Convert.ToInt32(parts[4]),
AVG = Convert.ToDouble(parts[5])
};
}
Then you would be able to create a List<Entry>
that contains the list of entries form the file and then you can work with them as you like. For example:
var entries =
openList()
.Select(x => CreateEntry(x))
.ToList();
var sorted_entries =
entries
.OrderByDescending(x => x.RBI)
.ToList();
Upvotes: 1