Converting a text file with two data point into an array

  • Thread starter Thread starter Mani25
  • Start date Start date
M

Mani25

Guest
I have a text file that looks like this:

Jane 56

john 45

In between the name data and the age data there is a tab "\t". This text file will be continually been written to.

this text file with unknown records needs to read and the data is placed into two array (one for Name, the other for marks) the the Class Average can be calculated when the stats button is clicked. Which where I am having issues. I don't know what is wrong with my code. When a I click the button I get an exception thrown and I am not sure how to fix it. I am posting my code could some tell what I need to fix?

sing System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace test8
{
public partial class Form1 : Form
{

int intStuNum = 0;
string strLine = "";
string[] strName;
double[] dblMark;
int intLastBlank;
double dblClassAverage;
int i = 0;
int j = 0;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{


string strFilename; // Name of file to be opened

strFilename = @"C:\CMIS214\StudentMarks.txt";





if (File.Exists(strFilename))
{
StreamReader srReader = new StreamReader(strFilename);



try
{
while (!srReader.EndOfStream)
{
intStuNum = intStuNum + 1;


strLine += srReader.ReadLine();


}
txtStudentMarks.Text += strLine;
txtDisplay.Text = Convert.ToString(intStuNum);

if (intStuNum <= 0)
{
strName = new string[intStuNum];
dblMark = new double[intStuNum];

for (j = 0; j <= intStuNum-1; j++)
{
intLastBlank = strLine.IndexOf("\t");

strName[intStuNum] = strLine.Substring(0, intLastBlank);

dblMark[intStuNum] = Convert.ToDouble(strLine.Substring(intLastBlank + 1));
}

}



}

catch
{
MessageBox.Show("Problem reading data from the file");
}
srReader.Close();
}
else
{
MessageBox.Show("File does not exist");
}

}

private void btnStats_Click(object sender, EventArgs e)
{

double dblTotal = 0;

for (int i = 0; i <= intStuNum - 1; i++)
dblTotal += dblMark;

dblClassAverage = dblTotal / intStuNum;
txtAvg.Text = Convert.ToString(dblClassAverage);
}
}
}

Continue reading...
 

Similar threads

L
Replies
0
Views
160
LearningVisualC2005
L
D
Replies
0
Views
126
DenisAndreevich
D
Back
Top