Solved

Load data to 2D array c#

Posted on 2011-03-01
4
344 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
Comment Utility
txt File in here......
feature143.txt
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
Comment Utility
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
Comment Utility
Thank you. worked great!
0
 

Author Comment

by:dadadude
Comment Utility
starting to like c# again. lol
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 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

13 Experts available now in Live!

Get 1:1 Help Now