CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2010
    Posts
    2

    how to retrieve/print a column from string array in C#

    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

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: how to retrieve/print a column from string array in C#

    You'll need to know that you can split strings (delimited by spaces with):

    Code:
    string example = "hello world blah blah 2";
    string[] splitArray = example.split(new char[] { ' ' });
    
    //Split array now has the following contents:
    // splitArray[0] = hello;
    // splitArray[1] = world;
    // splitArray[2] = blah;
    // splitArray[3] = blah;
    // splitArray[4] = 2;
    You can also parse integers with the Int32.Parse(int) method or parse doubles with Double.Parse(double) method.

    That should be enough to get you there! Good luck!
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured