Eduard
Eduard

Reputation: 7

Separate values into columns

I want to separate values into columns. The 3 columns are: EmployeeId, Name, Salary.

I don´t want to use split method so I try this instead

  private static void Main(string[] args)
    {
        List<string> list1= new List<string>();
        List<string> list2= new List<string>();
        List<string> list3= new List<string>();



        var word= "";
        using (var rd = new StreamReader(@"C:\xxx.csv"))
        {
            while (!rd.EndOfStream)
            {

                var line = rd.ReadLine();
                for (int i = 0; i < line.Length; i++)
                {
                    if (line[i] != Convert.ToChar(","))
                    {
                        word= word+ line[i];
                    }
                    if (line[i] == Convert.ToChar(","))
                    {
                        list1.Add(word);
                        word= "";
                    }
                    if (i == (line.Length - 1))
                    {
                        list3.Add(word);
                    }
                }
            }
        }

        Console.WriteLine("Employee ID:");
        foreach (var num in list1)
            Console.WriteLine(num);

        Console.WriteLine("employeename:");
        foreach (var employeename in list2)
            Console.WriteLine(employeename);
        Console.WriteLine("Salary");
        foreach (var employeesalary in list3)
        {
            Console.WriteLine(employeesalary);
        }
        Console.ReadKey();
    }

}
}

The column employeeSalary gets assigned correctly, however, EmployeeName column receives all values (wrong) and EmployeeID does not contain anything. Can anyone help me to find the error?

Upvotes: 0

Views: 89

Answers (3)

Enigmativity
Enigmativity

Reputation: 117029

I know you asked not to use .Split(...), but why not? Your code could look like this:

string[][] data =
    File
        .ReadAllLines(@"C:\xxx.csv")
        .Select(line => line.Split(','))
        .ToArray();

List<string> list1 = data.Select(line => line[0]).ToList();
List<string> list2 = data.Select(line => line[1]).ToList();
List<string> list3 = data.Select(line => line[2]).ToList();

Upvotes: 1

CoolBots
CoolBots

Reputation: 4869

Here's a working version of your code. I added some comments, removed the unnecessary Convert.ToChar calls, and added curly braces around prints, just as general code cleanup. Notice the else-if within the loop as well, as some of the situations are mutually exclusive. My answer is inline with my comments on your original post.

    private static void Main(string[] args)
    {
        var employeeIDs = new List<string>();
        var employeeNames = new List<string>();
        var employeeSalaries = new List<string>();


        var tmp = "";
        using (var rd = new StreamReader(@"values.csv"))
        {
            while (!rd.EndOfStream)
            {
                var line = rd.ReadLine();
                bool isFirstColumn = true;          //Indicates we are looking for first column value at this time

                for (int i = 0; i < line.Length; i++)
                {
                    if (line[i] != ',')
                    {
                        tmp = tmp + line[i];
                    }
                    else if (line[i] == ',')
                    {
                        if (isFirstColumn)
                        {
                            //This is the ID, because isFirstColumn is still set to true
                            employeeIDs.Add(tmp);
                            isFirstColumn = false;      //Next "," separates the name
                        }
                        else
                        {
                            //This is the name, because isFirstColumn is now false
                            employeeNames.Add(tmp);
                        }

                        tmp = "";
                    }

                    if (i == (line.Length - 1))
                    {
                        employeeSalaries.Add(tmp);
                        tmp = "";
                    }
                }
            }
        }

        Console.WriteLine("Employee ID:");
        foreach (var num in employeeIDs)
        { 
            Console.WriteLine(num);
        }

        Console.WriteLine("name:");
        foreach (var name in employeeNames)
        {
            Console.WriteLine(name);
        }

        Console.WriteLine("Salary");
        foreach (var salary in employeeSalaries)
        {
            Console.WriteLine(salary);
        }

        Console.ReadKey();
    }

Upvotes: 0

Hari Prasad
Hari Prasad

Reputation: 16956

I could quickly thin of this solution if I asked to do this with simple loops and not using any string functions.

Use this logic inside while loop, probably you might want to move List declaration to before while.

bool firstcolumn= false, secondcolumn = false, thirdcolumn= false;

System.Text.StringBuilder sb = new System.Text.StringBuilder();
List<string> list1 = new List<string>();
List<string> list2 = new List<string>();
List<string> list3 = new List<string>();
for (int i = 0; i < line.Length; i++)
{
    if(line[i] != ',' && i != line.Length -1) 
    {
        sb.Append(line[i]);
        continue;
    }

    if(!firstcolumn)
    {
        firstcolumn = true;
        list1.Add(sb.ToString());

    }
    else if(!secondcolumn)
    {
        secondcolumn = true;
        list2.Add(sb.ToString());

    }
    else
    {
        thirdcolumn = true;
        list3.Add(sb.ToString());
    }

    sb.Clear();     
}

Working Demo

Upvotes: 0

Related Questions