Hello, I have a text file that I need to convert the list of numbers into integers. So far I'm just reading it in real basic(I've tried a lot of code before I came here, I usually do, but I'm still a noob/boob). I have two columns rating and frequency, each with about 5 numbers below them. I can read the file into a list box just fine into the windows forms using:

private void ReadButton1_Click(object sender, EventArgs e)
        {
            List<string> myList = new List<string>();
            myList = System.IO.File.ReadLines("f:\\numbers.txt").ToList();
            this.listBox1.DataSource = myList;
        }

How do I convert the numbers in the text to ints? It seemed easy at first, but it isn't when I don't know how(I've seen some discussions on how to do this in a console, but that gave me more problems than I knew what to do with)....all I know is I need to use the streamreader and split method to do this, then convert. Any help is appreciated. Hopefully one day I won't be the one asking questions ALL the time and I can start providing help to someone like me.

Can you give us an example of the layout of the file?
Basically, you split the incoming strings into numeric string segments and then call Integer.Parse() or Conver.ToInt16() on them.

I've got it figured out. Here is what I have:

`using System;


    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;

    namespace ReadNumbersText
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            /// <summary>
            /// Read Button(pulls text from txt file, 
            /// converts to int32, and prints in listbox)
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                List<int> responses = new List<int>();
                int[] frequency = new int[15];

                try
                {
                    using (StreamReader readFile = new StreamReader("f:\\numbers.txt"))
                    {
                        string line;
                        string[] row = new string[1];

                        while ((line = readFile.ReadLine()) != null)
                        {
                            row = line.Split(',');
                        }
                        for (int i = 0; i < row.Length; i++)
                        {
                            responses.Add(Convert.ToInt32(row[i]));
                        }
                    }
                }
                catch (IOException ioEx)
                {
                    MessageBox.Show(ioEx.Message);
                }

                for (int answer = 0; answer < responses.Count; answer++)
                {
                    try
                    {
                        ++frequency[responses[answer]];
                    }
                    catch (IndexOutOfRangeException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                this.listBox1.Items.Add(string.Format("{0} {1}", "Rating", "Frequency"));
                for (int rating = 1; rating < frequency.Length; rating++)
                    this.listBox1.Items.Add(string.Format("{0,6}{1,10}", rating, frequency[rating]));
            }
        }
    }`
    This works good enough for me!=)

the layout is a 2 column 15 row output.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.