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>();
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.

2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by kiail

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;

{
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
{
{
string line;
string[] row = new string[1];

{
row = line.Split(',');
}
for (int i = 0; i < row.Length; i++)
{
}
}
}
catch (IOException ioEx)
{
MessageBox.Show(ioEx.Message);
}

{
try
{
}
catch (IndexOutOfRangeException ex)
{
MessageBox.Show(ex.Message);
}
}