siemprepeligroso
Newbie level 6
Dear friends,
I was trying to do some task in C# but now I am stuck
first I should read some data from a text file and arrange them i form of a rows
later I just needed to show the content of row number 5
after this I had to find the average of column 4 (by converting data from string to integer)
and in the end print out the column 4
I did manage to do first 3 task but I am not able to print our the 4th column
here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HW0_DM
{
class ReadFile
{
static void Main(string[] args)
{
// Read the file lines into a string array.
string[] row = System.IO.File.ReadAllLines(@"C:\testfile.txt");
System.Console.WriteLine("Contents of testfile.txt =:");
System.Console.WriteLine("\n\tName\tSurname\tPOS\tAge\n");
foreach (string line in row)
{
Console.WriteLine("\t" + line);
}
Console.WriteLine("\nResult of row 5\n\n\t" + row[4]);
var ages =
from column in row
let a = column.Split('\t')
select Convert.ToInt32(a[3]);
var results = ages.ToArray();
//average of 4th column
double avgage = results.Average();
Console.WriteLine("\nResult of column 4\n\n\t");
Console.Write(results);
Console.Write("\n\nAverage Team Age:\t");
Console.Write(avgage + "\n");
// Keep the console window open in debug mode.
Console.WriteLine("\nPress any key to exit.");
System.Console.ReadKey();
}
}
}
HOW CAN I PRINT TO THE USER CONSOLE THE CONTENT OF COLUMN 4
here is the content of the text file:
Theo Walcott FWD 22
Tomas Rosicky CMF 24
Bacary Sagna LB 27
Gael Clichy RB 26
Abou Diaby CMF 21
Kolo Toure CB 26
Reply With Quote
I was trying to do some task in C# but now I am stuck
first I should read some data from a text file and arrange them i form of a rows
later I just needed to show the content of row number 5
after this I had to find the average of column 4 (by converting data from string to integer)
and in the end print out the column 4
I did manage to do first 3 task but I am not able to print our the 4th column
here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HW0_DM
{
class ReadFile
{
static void Main(string[] args)
{
// Read the file lines into a string array.
string[] row = System.IO.File.ReadAllLines(@"C:\testfile.txt");
System.Console.WriteLine("Contents of testfile.txt =:");
System.Console.WriteLine("\n\tName\tSurname\tPOS\tAge\n");
foreach (string line in row)
{
Console.WriteLine("\t" + line);
}
Console.WriteLine("\nResult of row 5\n\n\t" + row[4]);
var ages =
from column in row
let a = column.Split('\t')
select Convert.ToInt32(a[3]);
var results = ages.ToArray();
//average of 4th column
double avgage = results.Average();
Console.WriteLine("\nResult of column 4\n\n\t");
Console.Write(results);
Console.Write("\n\nAverage Team Age:\t");
Console.Write(avgage + "\n");
// Keep the console window open in debug mode.
Console.WriteLine("\nPress any key to exit.");
System.Console.ReadKey();
}
}
}
HOW CAN I PRINT TO THE USER CONSOLE THE CONTENT OF COLUMN 4
here is the content of the text file:
Theo Walcott FWD 22
Tomas Rosicky CMF 24
Bacary Sagna LB 27
Gael Clichy RB 26
Abou Diaby CMF 21
Kolo Toure CB 26
Reply With Quote