私は、同じテキスト ファイルにあるサッカー リーグと備品を読み取るプログラムを作成しています。リーグ情報は上位 32 行で、備品はドキュメントの残りの部分です。とにかく、プログラムを実行して、リストボックスに表示されるリーグ情報 (名前、スポンサーなど) だけを取得しようとすると、テキスト全体からランダムなコード行が取得されます。上位32行のみが表示されるように修正する方法はありますか?
public partial class frmLeaguesAndFixtures : Form
{
public static frmLeagues frmkeepLeagues = null;
ArrayList leaguesArray = new ArrayList();
public static string inputDataFile = @"E:\Soft130\CW\SOFT130GroupAssignment\SOFT130GroupAssignment\Assignment\bin\Debug\football.txt";
const int numLeagueItems = 8;
public frmLeaguesAndFixtures()
{
InitializeComponent();
}
private bool fileOpenForReadOK(string readFile, ref StreamReader dataIn)
{
try
{
dataIn = new StreamReader(readFile);
return true;
}
catch (FileNotFoundException notFound)
{
MessageBox.Show("ERROR Opening file (when reading data in) - File could not be found.\n"
+ notFound.Message);
return false;
}
catch (Exception e)
{
MessageBox.Show("ERROR Opening File (when reading data in)- Operation failed.\n"
+ e.Message);
return false;
}
}
public static bool getNextLeague(int numItems, StreamReader inNext, string[] nextLeagueData)
{
//locals
string nextLine;
int numDataItems = numItems;
//read nextdata - based on constant numDataItems
for (int i = 0; i < numDataItems; i++)
{
try
{
nextLine = inNext.ReadLine();
if (nextLine != null)
nextLeagueData[i] = nextLine;
else
{
return false; //no more full data to process
}
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show("ERROR Reading from file. Incomplete Data.\n" + e.Message);
return false; //problem - could not read file
}
}
return true;//no problems
}
private void readLeague()
{
string inLeagueName, string inLeagueSponsor, int inLeaguePrize, int inLeagueNumFixtures)
//local variables
StreamReader inLeague = null;
Leagues tempLeague;
bool anyMoreLeagues = false;
string[] leagueData = new string[numLeagueItems];
//if file opened ok proceed
if (fileOpenForReadOK(inputDataFile, ref inLeague))
{
//read first league
anyMoreLeagues = getNextLeague(numLeagueItems,inLeague, leagueData);
//loop for all FULL league in file
while (anyMoreLeagues == true)
{
//create new league
tempLeague = new Leagues(leagueData[0], leagueData[1], leagueData[2], leagueData[3]);
//store in array
leaguesArray.Add(tempLeague);
//DOESNT WORK CORRECTLY TRY IT USING "PREMIERSHIP"
// if (leagueData[0] == "The Championship")
{
foreach (Leagues l in leaguesArray)
{
lstLeagueInfo.Items.Add(l.getLeagueName() + " " + l.getLeagueSponsor() + " " + l.getLeaguePrize() + " " + l.getLeagueNumFixtures());
}
}
//read next data
anyMoreLeagues = getNextLeague(numLeagueItems,inLeague, leagueData);
} //end while - no more
}//end if file ok
}
private void btnPremiership(object sender, EventArgs e)
{
readLeague();
}
}