Trying to read a .txt file which is a list of integers and insert them into an array.
Says there is an unhandled exception in the while loop.
{
public partial class Form1 : Form
{
private StreamReader inFile;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int[] grades = new int[40];
int countGrades = 0;
int totalScores = 0;
int arrayIndex = 0;
double avgGrade = 0;
string inValue;
if (File.Exists("Grades.txt"))
{
inFile = new StreamReader("Grades.txt");
}
else
{
lblMsg.Text = "File not found";
}
for (int i = 0; i-1 < countGrades; i++)
{
while ((inValue = inFile.ReadLine()) != null)
{
grades[arrayIndex++] = Convert.ToInt32(inValue);
}
grades[i] = Convert.ToInt32(inValue);
totalScores += grades[i];
countGrades++;
}
lblMsg.Text = Convert.ToString(grades[0]);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
inFile.Close();
}
}
Edited by Alexander, 04 November 2010 - 06:19 PM.
Added [code] tags.


Sign In
Create Account

Back to top









