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

  • Home
  • SEARCH
  • 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 7858709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:34:14+00:00 2026-06-02T21:34:14+00:00

I am trying to read a file into a global array, loop through the

  • 0

I am trying to read a file into a global array, loop through the array and identify each string to it’s corresponding amount, and then find the sums in their own groups. and divide those groups each by their own unique number.

The text file that I am reading in:

Name,50
Name,40
DifferentName,50
AnotherName,10
Name,60

What would be the easiest way to split the lines at the commas, and make the numbers correspond to their specific names, not knowing which order they will be in?

Here is the code I am using so far..although right now it is just the open file dialog, I am putting this here as a constructive reference.

        string strFileName;

    private void btnReadInFile_Click(object sender, EventArgs e)
    {
        //select the file
        OpenFileDialog ofdGetFile = new OpenFileDialog();
        if (ofdGetFile.ShowDialog() == DialogResult.Cancel)
        {
            //cancel
            MessageBox.Show("User Canceled");
            return;
        }
        else
        {
            //open the file
            StreamReader myStreamReader;
            strFileName = ofdGetFile.FileName;
            FileStream input = new FileStream(strFileName, FileMode.Open, FileAccess.Read);
            myStreamReader = new StreamReader(input);
            // other
            MessageBox.Show("Reading Complete", "Done!", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }

    private void btnShowGrade_Click(object sender, EventArgs e)
    {
        if (strFileName != null)
        {
            String[][] nameSums = System.IO.File.ReadLines(strFileName)
           .Select(l => l.Split(','))                // split the line
           .GroupBy(arr => arr[0])                   // group by name
           .Select(grp => new String[]{
            grp.Key,
            grp.Sum(arr => int.Parse(arr[1])).ToString()})
           .ToArray();

        }
        else
        {
            MessageBox.Show("You need to read in a file first.");
        }
    }
}

I feel like there has to be a better way to do this.

Thank you all so much for your help thus far! I am sure that the only reason this problem hasn’t been resolved is my lack of communication skills.

  • 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-06-02T21:34:18+00:00Added an answer on June 2, 2026 at 9:34 pm
    myArray = File.ReadAllLines(myFileName).Select(l => l.Split(',')).ToArray();
    

    Note this will be a staggered array, not a 2 dimensional one. To create a 2 dimensional one, you could use

    lines = File.ReadAllLines(myFileName).Select(l => l.Split(',')).ToArray();
    myArray = new string[lines.Count(), 2];
    for(int i = 0; i < lines.Length; i++)
    {
        myArray[i, 0] = lines[i, 0];
        myArray[i, 1] = lines[i, 1];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read a .wav file into an array of bytes to be
I am trying to read a simple text file into a String. Of course
So I am trying to read an XML file into a string in Perl
I am trying to read a binary file into an array of structure struct
i am trying to read jpg file and convert into byte array this is
I'm trying to read an xml file into memory, add a node, then write
I'm trying to read a csv file into R that has date values in
I am trying to read in 10 signed integers from a file into an
I'm trying to read data from a binary file and put it into a
I'm trying to read a file line by line to a string type variable

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.