Reading CSV file and storing values into an array

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Reading CSV file and storing values into an array

Reading CSV File and Storing Values into an Array: A Complete Guide

πŸ“š Welcome to another exciting edition of our tech blog! Today, we'll tackle a common issue many developers face: reading a CSV file and storing its values into an array. πŸ’ΎπŸ“Š

The Problem

πŸ€” So, you want to read a CSV file, huh? Well, you've come to the right place! The first step is to understand the structure of the CSV file you're dealing with. In this case, the file has two columns separated by semicolons. Let's dive into the solution!

The Solution

πŸŽ‰ To read the CSV file and store its values into separate arrays, follow these simple steps:

1️⃣ Open the CSV file using the StreamReader class, which allows you to read text from a file.

2️⃣ Create two empty arrays, one for each column of the CSV file.

3️⃣ Loop through each line of the CSV file and split it using the Split() function, passing the semicolon as the delimiter.

4️⃣ After splitting the line, retrieve the values for each column and store them in their corresponding arrays.

5️⃣ Finally, display the content of each array.

Here's a code example in C# that demonstrates the solution:

using System;
using System.IO;

public class CSVReader
{
    public static void Main()
    {
        string filePath = "path_to_csv_file.csv"; // Replace with the actual file path

        string[] column1Array = new string[100]; // Adjust the array size as needed
        string[] column2Array = new string[100]; // Adjust the array size as needed

        using (StreamReader reader = new StreamReader(filePath))
        {
            string line;
            int i = 0;

            while ((line = reader.ReadLine()) != null)
            {
                string[] values = line.Split(';');

                column1Array[i] = values[0];
                column2Array[i] = values[1];

                i++;
            }
        }

        // Display the content of each array
        Console.WriteLine("Column 1:");
        foreach (string value in column1Array)
        {
            if (value != null)
                Console.WriteLine(value);
        }

        Console.WriteLine("Column 2:");
        foreach (string value in column2Array)
        {
            if (value != null)
                Console.WriteLine(value);
        }
    }
}

Make sure to replace "path_to_csv_file.csv" with the actual path to your CSV file.

πŸ“£ Encouraging Reader Engagement

🌟 We hope you found this guide helpful for reading a CSV file and storing its values into separate arrays. Now it's time for you to try it out in your own projects!

πŸ“’ Let us know in the comments below if you face any issues or if you have any related questions. We're always here to help you out! Happy coding! πŸ’»πŸ’ͺ

Call to Action

πŸ“’ Share this blog post with your fellow developers, so they can also master the art of reading CSV files and storing values into arrays. Together, let's make the world of coding a better place! πŸŒπŸš€

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

πŸ”₯ πŸ’» πŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! πŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings πŸ’₯βœ‚οΈ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide πŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? πŸ€” Well, my