I am trying to make a trivia game in c# using a console application. And I am having troubles getting the console to read the file. Currently what its doing is saying that the file could not be read and the the index was outside the bounds of the array. But then everything that is in the text file gets displayed. I am unsure on what I get the file could not be read but then the file gets displayed.
The file is a .txt file and here is what is looks like
What is another name for SuperMan?,the man of steel
What is Superman's only weakness?,kryptonite
What is the name of Batman's secret identity?,bruce wayne
Batman protects what city?,gotham city
How did Spiderman get his superpowers?,bitten by a radioactive sipder
This superheros tools include a bullet-proof braclets and a magic lasso.Who is she?,wonder woman
Which superhero has an indestructible sheild?,captain america
Which superhero cannot transformback into human form?,the thing
What villan got his distinctive appearance form toxic chemicals?,joker
What is the name of the archnemesis of the Fantastic Four?, dr doom
Here is the code that I have for reading and displaying the file.
static void Main(string[] args)
{
string filename = @"C:\Trivia\questions.txt";
List<string> questions = new List<string>();
List<string> answers = new List<string>();
LoadData(filename, questions, answers);
Console.WriteLine();
questions.ForEach(Console.WriteLine);
Console.WriteLine();
answers.ForEach(Console.WriteLine);
}
static void LoadData(string filename, List<string> questions, List<string> answers)
{
try
{
using(StreamReader reader = new StreamReader(filename))
{
string line;
while((line = reader.ReadLine()) != null)
{
string[] lineArray = line.Split(',');
string annswer = lineArray[1];
string question = lineArray[0];
questions.Add(question);
answers.Add(annswer);
}
}
}
catch(Exception e)
{
Console.WriteLine("File could not be read");
Console.WriteLine(e.Message);
}
}
Here is the output on the console.
File could not be read
Index was outside the bounds of the array.
What is another name for SuperMan?
What is Superman's only weakness?
What is the name of Batman's secret identity?
Batman protects what city?
How did Spiderman get his superpowers?
This superheros tools include a bullet-proof braclets and a magic lasso.Who is she?
Which superhero has an indestructible sheild?
Which superhero cannot transformback into human form?
What villan got his distinctive appearance form toxic chemicals?
What is the name of the archnemesis of the Fantastic Four?
the man of steel
kryptonite
bruce wayne
gotham city
bitten by a radioactive sipder
wonder woman
captain america
the thing
joker
dr doom
Thanks for the suggestions.

string annswer = lineArray[1];(you have hard-coded index1without first checking the size of thelineArray). However, in that case no other output would be shown than the error message. When I copy/paste your code and sample file contents, it works correctly, so something is different on your machine - either the code, the file contents, or both.