I have a text file containing the following content:

0 12
1 15
2 6
3 4
4 3
5 6
6 12
7 8
8 8
9 9
10 13

There are no spaces between two rows but there is a space between two numbers. I want to read these integers from a txt file and save the two columns into two different arrays in C# and then i have to display array values.Can anyone help?

You could use a third array to hold the lines of strings. For each line in the string array .Split()[0] gets parsed to an int and put in array1, and .Split()[1] gets parsed as an int and put into array2.

Here's a link to the C# Programming Guide

When you've read the resource material and attempted some code let us know when you have specific code that doesn't work.

Edited 3 Years Ago by tinstaafl

Read in the values from the fiile, using what ddanbe gave you to get you started.

Now can I ask you this. I assume the numbers are on one line. The file you are trying to read in, how exactly does it look? I know a great way to split strings up between arrays but need to see what you are working with (String parsing is one of my specialties)

Hope to hear back soon

Each line has two numbers which are integers and no decimal numbers.the first column specifies the x-coordinate and other represents the y-coordinate.there is space between two numbers but there is no space between two lines.i have written the following code but it raises an error saying "Input string was not in a correct format".Can u help me with the code?

private void btnrecog_Click(object sender, RoutedEventArgs e)
        {
            List<int> column0  = new List<int>(), column1 = new List<int>();

            using (Stream stream = File.Open("abcdaslak.txt", FileMode.Open))
            using (TextReader sr = new StreamReader(stream, Encoding.UTF8))
                    {
                        string line;
                        while ((line = sr.ReadLine()) != null)
                                     {
                                        string[] arr = line.Split(' ');
                                        column0.Add(Int32.Parse(arr[0]));
                                        column1.Add(Int32.Parse(arr[1]));
                                     }
                    }

            for (int i = 0; i < column0.Count; i++)
            {
                Console.WriteLine(column0[i]);
                Console.WriteLine(column1[i]);
            }


        }

Tried your code, and had no problem.
Just wondering why you are using Console.WriteLine. If it is for testing, I debug or I use MessageBox.Show.

You better check your text file, when I run your code using the numbers in your original post, it works fine. Except that I put the numbers into a listbox, since you can't normally show a console in a form app. If you still get an error try this:

        private void button1_Click(object sender, EventArgs e)
        {
            List<int> column0 = new List<int>(), column1 = new List<int>();
            foreach (string line in File.ReadAllLines(@"C:\abcdaslak.txt"))
            {

                column0.Add(GetInt(line.Split()[0]));
                column1.Add(GetInt(line.Split()[1]));
            }
            for (int i = 0; i < column0.Count; i++)
            {
                listBox1.Items.Add(column0[i] + " " + column1[i]);
            }            

        }
        private Int32 GetInt(string TempString)
        {
            Int32 tempint = 0;
            bool GoodParse = Int32.TryParse(TempString, out tempint);
            if (GoodParse)
            {
                return tempint;
            }
            else
            {
                MessageBox.Show(TempString);
                return -1;
            }
        }

Any string that can't be parsed will be shown in a messagebox and the value displayed in the listbox will be -1

Edited 3 Years Ago by tinstaafl

Comments
Shows knowledge.

@tinstaafl thank you so much.ur code works fine but still can't figure out whats the problem with my code.i think we cant use console with WPF applications

No, there is no visible console window for WPF applications. You would need to define a control on your display window and place the output there.

Is there any particular reason you're using WPF for something this simple? It's not really appropriate to the task.

actually i'm working on a project and aim is to develop an application that can recognize and redraw circuits which are drawn with a stylus pen on a tablet PC.So the above code is a part of recognition.i need to find the slope of the line drawn on the inkcanvas.if its slope is zero then we can recognize it as a wire.

so what i'm doing is putting the x and y coordinate in separate lists and then i can find the slope using

for (int i=0; i<=column1.Count - 2;i++)
{
  float slope = (column1[i+1] - column1[i])/(column0[i+1] - column0[i])
}

Thats the plan! if u have any new idea do share.

If(as I guess) column1 reprsents the Y-coordinate and column0 the X-coordinate, why don't you call them as such? A column can be anything a coordinate is a coordinate.
Why are you using a for loop? The slope will be calculated with the last point.

yes you are right about the slope but then it would be difficult to differentiate between a wire and a resistance. A wire( straight line) will have same slope throughout unlike resistor.thus i need to calculate slope at every point

I'm a bit older then you I guess and also a bit shaky sometimes. So by moments my strait line drawings would also have a slope != zero. How you are going to detect the difference?

This article has been dead for over six months. Start a new discussion instead.