TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Francisca Fraser
NA
12
1.2k
Hello, help needed
May 4 2018 9:18 AM
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 <words.Length; i++)
BT.Insert(words[i], count);
line = reader.ReadLine();
reader.Close();
}
catch (IOException e)
{
Console.WriteLine("" + e.ToString());
BT.Display();
Console.ReadLine();
Reply
Answers (
2
)
1
Prasham Sabadra
85
22.3k
3.2m
May 4 2018 11:10 AM
Hi,
I tried this in console application. If you intialise your words variable build will be successful as string words = {}
But then this program wont work properly. Coupleof things or best practice - Use the {} after while and for loop in the above program and then it works properly.
Test program is -
public class Program
{
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
Accepted Answer
0
Francisca Fraser
0
12
1.2k
May 4 2018 3:11 PM
Thanks Prasham, it worked
Get Row Cell Value From Grid View of Checked Checkbox in mvc
Help needed urgently: Performing searches using regex