Reputation: 33
I am new at programming in C# and I'm trying to read files from txt, however everytime this piece of code is executed I get index out of limit, even though I did debug I can't seen to find a solution for this case.
I'm sure if it's something easy, but I'm new at C#, thanks for looking at the code.
static void importFiles(string[,] matrix)
{
var path = @"export/file.txt";
int start = getInsertIndex(matrix);
if (File.Exists(path))
{
string[] fileLines = File.ReadAllLines(path);
if (fileLines.Length == matrix.GetLength(0))
{
string[,] map = new string[fileLines.Length, matrix.GetLength(1)];
for (int i = 0; i < fileLines.Length; i++)
{
string line = fileLines[i];
for (int j = 0; j < matrix.GetLength(1); j++)
{
string[] split = line.Split(';');
matrix[start, j] = split[j]?.Trim();
}
start++;
}
} }
static int getInsertIndex(string[,] matrix)
{
for (int j = 0; j < matrix.GetLength(0); j++)
{
if (string.IsNullOrEmpty(matrix[j, 0])) return j;
}
return -1;
}
I've changed the code, however when use a nested for to visualize what's inside the matrix, I got nothing. I can't seem to understand why the method is executed and I get nothing inside the matrix.
Upvotes: 0
Views: 39
Reputation: 4313
Seems to me that you do not know upfront the lines of your file and you do not know the amount of items when splitting the line. In this case, I would not go with the fixed matrix but with a dictionary or alike.
static void Main(string[] args)
{
Dictionary<int, string[]> filesData = new Dictionary<int, string[]>();
importFiles(filesData);
}
static void importFiles(Dictionary<int, string[]> filesData)
{
var path = @"export/file.txt";
if (File.Exists(path))
{
string[] fileLines = File.ReadAllLines(path);
for (int i = 0; i < fileLines.Length; i++)
{
string line = fileLines[i];
string[] split = line.Split(';');
for (int j = 0; j < split.Length; j++)
{
split[j] = split[j].Trim();
}
int index = filesData.Count == 0 ? 0: filesData.Keys.Max();
filesData.Add(index + 1, split);
}
}
}
Upvotes: 1
Reputation: 37337
You could handle situation in your code if number of rows in matrix is different from number of lines read (throw exception for example):
static void importFiles(string[,] matrix)
{
var path = @"export/file.txt";
if (!File.Exists(path)) return;
string[] fileLines = File.ReadAllLines(path);
if(fileLines.Length != matrix.GetLength(0))
// here you have couple of opitions
// throw new ArgumentException("Number of rows in passed matrix is different from number of lines in a file!");
// or just do nothing:
return;
string[,] map = new string[fileLines.Length, fileLines[0].Split(';').Length];
for (int i = start; i < fileLines.Length; i++)
{
string line = fileLines[i];
for (int j = 0; j < matrix.GetLength(1); j++)
{
string[] split = line.Split(';');
matrix[i, j] = split[j]?.Trim();
}
}
}
Or you could initilize your matrix
array in your method:
string[] fileLines = File.ReadAllLines(path);
// c is number of columns, that you are using now
string[,] matrix = new string[fileLines.Length, c];
Upvotes: 1