C
C#14mo ago
Dinny

✅ Reading matricies to an output file

I am trying to read the data inside two matricies from an input file firstly in order to add them together and display the result to the output file. Before I get to that, I am struggling trying to get the program to read each row and column. Here's the block of code with my errors
//going through each row to get matrix numbers
while (!reader.EndOfStream)
{

nextLine = reader.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
if (nextLine.Length == numOfColumns)
{
for (int j = 0; j < numOfColumns; j++)
{

a[rowIndex, j] = int.Parse(nextLine[j]);

}

rowIndex++;


}
}
//going through each row to get matrix numbers
while (!reader.EndOfStream)
{

nextLine = reader.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
if (nextLine.Length == numOfColumns)
{
for (int j = 0; j < numOfColumns; j++)
{

a[rowIndex, j] = int.Parse(nextLine[j]);

}

rowIndex++;


}
}
I have a red line where it says " a[rowIndex, j] = int.Parse(nextLine[j]); and the error reads "Cannot apply indexing [] to an expression of type "Matrix.Matricies."
5 Replies
Dinny
DinnyOP14mo ago
ok so i’ve gotta rid of the red line, but now it’s u def the “int.Parse(nextLine[j])” except i replaced the Parce() with Convert.ToUint32
Omnissiah
Omnissiah14mo ago
so a is a what? data is what?
Esa
Esa14mo ago
This usually works well enough for me:
const string path = "C:/path/to/file.csv"; // path to your file
const char delimiter = ','; // insert your actual delimiter here

File.ReadAllLines(path)
.Select(row => row.Split(delimiter, StringSplitOptions.RemoveEmptyEntries))
.Select(columns =>
{
// Normally I add explicit validation, but for a quick and naive approach this works
int? column1 = int.TryParse(columns[0], out int result0) ? result0 : null;
int? column2 = int.TryParse(columns[1], out int result1) ? result1 : null;
int? column3 = int.TryParse(columns[2], out int result2) ? result2 : null;

// execute your logic in here
return "ok";
});
const string path = "C:/path/to/file.csv"; // path to your file
const char delimiter = ','; // insert your actual delimiter here

File.ReadAllLines(path)
.Select(row => row.Split(delimiter, StringSplitOptions.RemoveEmptyEntries))
.Select(columns =>
{
// Normally I add explicit validation, but for a quick and naive approach this works
int? column1 = int.TryParse(columns[0], out int result0) ? result0 : null;
int? column2 = int.TryParse(columns[1], out int result1) ? result1 : null;
int? column3 = int.TryParse(columns[2], out int result2) ? result2 : null;

// execute your logic in here
return "ok";
});
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server