我试图使用逗号作为分隔符从文本文件填充组合框一切正常,但现在当我调试时,我得到"索引超出范围异常未处理"警告.我想我需要一双新眼睛看看我哪里出错了,我评论了得到错误的行// Fname = fields [1];
private void xViewFacultyMenuItem_Click(object sender, EventArgs e)
{
const string fileStaff = "source\\Staff.txt";
const char DELIM = ',';
string Lname, Fname, Depart, Stat, Sex, Salary, cDept, cStat, cSex;
double Gtotal;
string recordIn;
string[] fields;
cDept = this.xDeptComboBox.SelectedItem.ToString();
cStat = this.xStatusComboBox.SelectedItem.ToString();
cSex = this.xSexComboBox.SelectedItem.ToString();
FileStream inFile = new FileStream(fileStaff, FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(inFile);
recordIn = reader.ReadLine();
while (recordIn != null)
{
fields = recordIn.Split(DELIM);
Lname = fields[0];
Fname = fields[1]; // this is where the error appears
Depart = fields[2];
Stat = fields[3];
Sex = fields[4];
Salary = fields[5];
Fname = fields[1].TrimStart(null);
Depart = fields[2].TrimStart(null);
Stat = fields[3].TrimStart(null);
Sex = fields[4].TrimStart(null);
Salary = fields[5].TrimStart(null);
Gtotal = double.Parse(Salary);
if (Depart == cDept && cStat == Stat && cSex == Sex)
{
this.xEmployeeListBox.Items.Add(recordIn);
}
recordIn = reader.ReadLine();
}
Source file --
Anderson, Kristen, Accounting, Assistant, Female, 43155
Ball, Robin, Accounting, Instructor, Female, 42723
Chin, Roger, Accounting, Full, Male,59281
Coats, William, Accounting, Assistant, Male, 45371
Doepke, Cheryl, Accounting, Full, Female, 52105
Downs, Clifton, Accounting, Associate, Male, 46887
Garafano, Karen, Finance, Associate, Female, 49000
Hill, Trevor, Management, Instructor, Male, 38590
Jackson, Carole, Accounting, Instructor, Female, 38781
Jacobson, Andrew, Management, Full, Male, 56281
Lewis, Karl, Management, Associate, Male, 48387
Mack, Kevin, Management, Assistant, Male, 45000
McKaye, Susan, Management, Instructor, Female, 43979
Nelsen, Beth, Finance, Full, Female, 52339
Nelson, Dale, Accounting, Full, Male, 54578
Palermo, Sheryl, Accounting, Associate, Female, 45617
Rais, Mary, Finance, Instructor, Female, 27000
Scheib, Earl, Management, Instructor, Male, 37389
Smith, Tom, Finance, Full, Male, 57167
Smythe, Janice, Management, Associate, Female, 46887
True, David, Accounting, Full, Male, 53181
Young, Jeff, Management, Assistant, Male, 43513
Run Code Online (Sandbox Code Playgroud)
为了那些不想看你发布的庞大代码的人,这里是相关的一点:
while (recordIn != null)
{
fields = recordIn.Split(DELIM);
Lname = fields[0];
Fname = fields[1]; // this is where the error appears
Run Code Online (Sandbox Code Playgroud)
鉴于您已经看到的异常,这基本上意味着recordIn不包含分隔符DELIM(逗号).我建议您明确检查预期的大小,并抛出一个异常,如果你得到一个不合适的行,给出更多细节.或者,如果它是一个空白行,正如其他人所建议的那样(并且确实看起来很可能),您可能只想跳过它.
或者,这是一个简短但完整的控制台应用程序,可以帮助您找到问题:
using System;
using System.IO;
class Test
{
static void Main()
{
string[] lines = File.ReadAllLines("source\\Staff.txt");
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
string[] fields = line.Split(',');
if (fields.Length != 6)
{
Console.WriteLine("Invalid line ({0}): '{1}'",
i + 1, line);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)