tags:

views:

244

answers:

6

I'm writing this small program to extract any number of email address from a text file. I am getting two errors, "Use of unassigned local variable." and I'm not sure why.

static void Main(string[] args)
{
string InputPath = @"C:\Temp\excel.txt";
string OutputPath = @"C:\Temp\emails.txt";
string EmailRegex = @"^(?:[a-zA-Z0-9_'^&/+-])+(?:\.(?:[a-zA-Z0-9_'^&/+-])+)*@(?:(?:\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)|(?:[a-zA-Z0-9-]+\.)+(?:[a-zA-Z]){2,}\.?)$";
string Text = String.Empty;
StringBuilder Output;
List<string> Emails;

using (TextReader tr = new StreamReader(InputPath))
{
 Text = tr.ReadToEnd();
}

MatchCollection Matches = Regex.Matches(Text,EmailRegex);

foreach (Match m in Matches)
{
 Emails.Add(m.ToString().Trim()); // one error is here
}

foreach (String s in Emails)
{
 Output.Append(s + ","); // the other error is here
}

using (TextWriter tw = new StreamWriter(OutputPath))
{
 tw.Write(Output.ToString());
}
}

Sorry for the formatting ... I'm kind of pressed for time!

edit: WOW. I'm an idiot - must be because I'm pressed for time!!!!

+21  A: 

You aren't initializing the StringBuilder and List.

StringBuilder Output = new StringBuilder();
List<string> Emails = new List<String>();
Dave Markle
+9  A: 

The problem is here:

StringBuilder Output;
List<string> Emails;

You haven't initialized Emails and Output. Try:

StringBuilder Output = new StringBuilder();
List<string> Emails = new List<string>();
lavinio
+8  A: 

you are not creating a Stringbuilder or Email List:

StringBuilder Output = new StringBuilder();
List<string> Emails = new List<string>();
Mitch Wheat
+8  A: 

Your "Output" and "List" variables are not assigned with object instances. Change:

StringBuilder Output;
List<string> Emails;

To

StringBuilder Output = new StringBuilder();
List<string> Emails = new List<string>();
d4nt
+4  A: 

You have to initialize the Emails and Output objects by using "new". Basically have:

StringBuilder Output = new StringBuilder();
List<string> Emails = new List<string>();
Zensar
+1  A: 

As so many have said, the errors come from the object that you didn't create.

But, why are you storing the email addresses in a temporary list, then putting them in a temporary string, then write it to a file? Just write them to the file directly:

static void Main(string[] args) {

   string InputPath = @"C:\Temp\excel.txt";
   string OutputPath = @"C:\Temp\emails.txt";
   string EmailRegex = @"^(?:[a-zA-Z0-9_'^&amp;/+-])+(?:\.(?:[a-zA-Z0-9_'^&amp;/+-])+)*@(?:(?:\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)|(?:[a-zA-Z0-9-]+\.)+(?:[a-zA-Z]){2,}\.?)$";

   using (TextWriter tw = new StreamWriter(OutputPath))
      foreach (Match m in Regex.Matches(File.ReadAllText(InputPath), EmailRegex)) {
         tw.Write(m.Value);
         tw.Write(',');
      }
   }
}

Note: The regular expression seems to have been corrupted somewhere along the line. There are some &amp; in it that probably should just be & characters.

Guffa