このコードでは、「インデックスが配列の境界外です」という例外がスローされています。これは分割された各データを指定されたアレイスロットに追加するだけでいいですか?.Split()メソッドで問題が発生しました
while (input != null)
{
string[] splitInput = inputLine.Split();
EmpNum = int.Parse(splitInput[0]);
EmpName = (splitInput[1]);
EmpAdd = (splitInput[2]);
EmpWage = double.Parse(splitInput[3]);
EmpHours = double.Parse(splitInput[4]);
inputLine = (myFile.ReadLine());
Console.WriteLine("test {0},{1},{2}", EmpNum, EmpWage, EmpHours);
}
ビットを明確にするために、私は、従業員データ(名前、住所、時間、従業員番号、賃金を)持っている、単純なテキストファイルからデータを読んでいます。
わかりやすくするために私の主な方法をすべて追加しました。
using System;
using System.IO;
class Program
{
static void Main()
{
//declare an array of employees
Employee[] myEmployees = new Employee[10];
//declare other variables
string inputLine;
string EmpName;
int EmpNum;
double EmpWage;
double EmpHours;
string EmpAdd;
//declare filepath
string environment = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\";
//get input
Console.Write("\nEnter a file name in My Documents: ");
string input = Console.ReadLine();
string path = environment + input;
Console.WriteLine("Opening the file...");
//read file
StreamReader myFile = new StreamReader(path);
inputLine = (myFile.ReadLine());
//split input
while (inputLine != null)
{
string[] splitInput = inputLine.Split();
EmpNum = int.Parse(splitInput[0]);
EmpName = (splitInput[1]);
EmpAdd = (splitInput[2]);
EmpWage = double.Parse(splitInput[3]);
EmpHours = double.Parse(splitInput[4]);
Console.WriteLine("test {0},{1},{2}", EmpNum, EmpWage, EmpHours);
}
Console.ReadLine();
}//End Main()
}//End class Program
はまた、私は私のコードの多くを投稿することができます。 – xavi
あなたの 'inputLine'を分割すると、5つ以下の要素が得られます – bjarneh
while(input!= null)' while(inputLine!= null) 'ではありませんか? –