C
C#8mo 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
Dinny8mo 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
life grinder
life grinder8mo ago
so a is a what? data is what?
Esa
Esa8mo 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
Accord8mo 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 User8mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server
More Posts
❔ How to Deploy WASM Project with .NET Backend in same repo?I have a Blazor frontend with a .NET backend. How do I use Azure to deploy these when they are in th❔ EF Core LINQ translation documentIn the process of upgrading a .NET Core 1.1 to 6 app, our EF Core LINQ queries are now hitting some Is there a better (or more appropriate way) of implementing method overloading for my usecase here?I have two methods of a class I'm writing - in one usecase I want to update the field with no return❔ Referencing new projects with the main projectLet's say I have a main project `CoreProject` and I want to add 2 new projects: a `LoggerService` an❔ Refactoring strategy pattern to a simpler codeI have the following code and I want te refactor it to a less-boiler-plate code. Wha'ts suposed to❔ Avoid "Authorizing..." in Blazor Page (Identity)I am trying to avoid the loading text "Authorizing..." when going to my index.razor page. I have add❔ Trying to return```csharp using System; using System.Drawing; using System.Windows.Forms; using AForge.Imaging; usin❔ Cancel task on nested exceptionI have a series of nested async methods, when a exception is thrown I want to cancel the task. The m✅ Newtonsoft serializing returns nothing with custom converterSo I have a custom `JsonConverter` from newtonsoft that is supposed to replace object properties tha❔ 415 error from HttpClient put to a controller using FromBody``` (await _client.PutAsync("foo", new StringContent(5.ToString()))).EnsureSuccessStatusCode(); ```