Solved

Load data to 2D array c#

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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