Solved

Load data to 2D array c#

Posted on 2011-03-01
4
345 Views
Last Modified: 2012-05-11
Hello all,
I have included a file.txt that i noticed it has 2 separators the tab and ' ' and i am having problems in loading the columns. that's my code.  I have like 1000 txt file of that type. I can't change them all.
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Collections.Generic;

namespace geneticAlgorithm
{
    class Program
    {
        static void Main(string[] args)
        {
            String [,] array2D;
            int numberOfLines = 0, numberOfColumns = 0;
            string line;
            String filename = "C:\\Decomposition\\Analyse\\image143\\feature143.txt";
            System.IO.StreamReader sr = new System.IO.StreamReader(filename);
            while((line = sr.ReadLine()) != null)
            {

                numberOfColumns = line.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries).Length;
                 numberOfLines++;
            }
            sr.Close();
            array2D = new String[numberOfLines, numberOfColumns];
            numberOfLines = 0;
           
            sr = new System.IO.StreamReader(filename);
            while ((line = sr.ReadLine()) != null)
            {
                
                String[] tempArray = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                Console.WriteLine(tempArray.Length);
                for (int i = 0; i < tempArray.Length; ++i)
                {
                   //array2D[numberOfLines, i] =tempArray[i];
                
             
                }
                numberOfLines++;
            }

          
            String Stop;
            Console.Write("press any Key to Stop program");
            Stop = Console.ReadLine();
        }
    }
}

Open in new window

0
Comment
Question by:dadadude
  • 3
4 Comments
 

Author Comment

by:dadadude
ID: 35005849
txt File in here......
feature143.txt
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 35005948
You can pass multiple delimiters to the split method.

i.e
numberOfColumns = line.Split(new char[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries).Length;

Open in new window

0
 

Author Closing Comment

by:dadadude
ID: 35005983
Thank you. worked great!
0
 

Author Comment

by:dadadude
ID: 35005991
starting to like c# again. lol
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now