I am a newly indoctrinated into C#, I wrote this code and its pulling an error (Use of unassigned local variable "words' ) in the line [for(int i = 0, i < words.Length; i++)], could you tell me where I went wrong
Thanks
This is the code
static void Main(string[] args)
{
BinaryTree BT = new BinaryTree();
string line; // will be used to read the file, one line at a time
int count = 1;
string[] words;
try
{
count = 1;
StreamReader reader = new StreamReader("testfile.txt");//create input stream
line = reader.ReadLine();//get the next line
while (line != null)
words = line.Split(' ');
for (int i = 0; i
BT.Insert(words[i], count);
line = reader.ReadLine();
reader.Close();
}
catch (IOException e)
{
Console.WriteLine("" + e.ToString());
BT.Display();
Console.ReadLine();
2 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Prasham SabadraPosted May 4, 2018, 11:10 AM
{
static void Main(string[] args)
{
string[] words = { };
string line;
try
{
StreamReader reader = new StreamReader("testfile.txt");
line = reader.ReadLine();
while (line != null)
{
words = line.Split(' ');
for (int i = 0; i < words.Length; i++)
{
Console.Write(words[i]);
line = reader.ReadLine();
}
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadKey();
}
Console.ReadKey();
}//main
}//cs
}//ns
Francisca FraserPosted May 4, 2018, 3:11 PM