Solved

Load data to 2D array c#

Posted on 2011-03-01
4
348 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

733 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