Solved

Load data to 2D array c#

Posted on 2011-03-01
4
349 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

728 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