Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6665143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:41:31+00:00 2026-05-26T02:41:31+00:00

I know how to read and display a line of a .csv file. Now

  • 0

I know how to read and display a line of a .csv file. Now I would like to parse that file, store its contents in arrays, and use those arrays as values for some classes I created.

I’d like to learn how though.

Here is an example:

basketball,2011/01/28,Rockets,Blazers,98,99
baseball,2011/08/22,Yankees,Redsox,4,3

As you can see, each field is separated by commas. I’ve created the Basketball.cs and Baseball classes which is an extension of the Sport.cs class, which has the fields:

private string sport;
private string date;
private string team1;
private string team2;
private string score;

I understand that this is simplistic, and that there’s better ways of storing this info, i.e. creating classes for each team, making the date a DateType datatype, and more of the same but I’d like to know how to input this information into the classes.

I’m assuming this has something to do with getters and setters… I’ve also read of dictionaries and collections, but I’d like to start simple by storing them all in arrays… (If that makes sense… Feel free to correct me).

Here is what I have so far. All it does is read the csv and parrot out its contents on the Console:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Assign01
{
    class Program
    {
        static void Main(string[] args)
        {
            string line;
            FileStream aFile = new FileStream("../../sportsResults.csv", FileMode.Open);
            StreamReader sr = new StreamReader(aFile);

            // read data in line by line
            while ((line = sr.ReadLine()) != null)
            {
                Console.WriteLine(line);
                line = sr.ReadLine();
            }
            sr.Close();
        }
    }
}

Help would be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:41:31+00:00Added an answer on May 26, 2026 at 2:41 am

    Creating array to keep the information is not a very good idea, as you don’t know how many lines will be in the input file. What would be the initial size of your Array ?? I would advise you to use for example a Generic List to keep the information (E.g. List<>).

    You can also add a constructor to your Sport Class that accepts an array (result of the split action as described in above answer.

    Additionally you can provide some conversions in the setters

    public class Sport
    {
        private string sport;
        private DateTime date;
        private string team1;
        private string team2;
        private string score;
    
        public Sport(string[] csvArray)
        {
            this.sport = csvArray[0];
            this.team1 = csvArray[2];
            this.team2 = csvArray[3];
            this.date = Convert.ToDateTime(csvArray[1]);
            this.score = String.Format("{0}-{1}", csvArray[4], csvArray[5]);
        }
    

    Just for simplicity I wrote the Convert Method, but keep in mind this is also not a very safe way unless you are sure that the DateField always contains valid Dates and Score always contains Numeric Values. You can try other safer methods like tryParse or some Exception Handling.

    I all honesty, it must add that the above solution is simple (as requested), on a conceptual level I would advise against it. Putting the mapping logic between attributes and the csv-file in the class will make the sports-class too dependent on the file itself and thus less reusable. Any later changes in the file structure should then be reflected in your class and can often be overlooked. Therefore it would be wiser to put your “mapping & conversion” logic in the main program and keep your class a clean as possible

    (Changed your “Score” issue by formatting it as 2 strings combined with a hyphen)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read data from a .log file and process its contents.
I know that read() is a blocking call unless I make the socket non-blocking.
So I know I read somewhere when the iPad 3 was released, that you
I have a data file, which I need to read. I know to read
Anyone know how to read Outlook nickname list file (.NK2)? Any document on how
Does anyone know how to read a x.properties file in Maven. I know there
Hi programmers, I want read line by line a Unicode (UTF-8) text file created
I am writing a web page that will display the content of a CSV
UPDATE: If anyone knows a tool that is free, and would display, and let
I am trying to display statistics from a simple text file using arrays in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.