Reputation: 93
Someone please help me with this code its showing array outofbond exception
.
datain file new_hosts.txt is like:
test test 28/6/2015 09:45 PM
test fafagf 30/6/2015 01:00 PM
test sfaasfag 28/6/2015 10:05 PM
code:
public void notification()
{
string regvalue;
int ctr = 0;
bool isNotificationExits=false;
string[] meetingdata=new string[]{};
string[] notificationsdata = new string[]{};
try
{
//ctr = File.ReadAllLines("new_hosts.txt").Length;
meetingdata = File.ReadAllLines("new_hosts.txt");
}
catch (FileNotFoundException)
{
//showmainmenu();
}
List<String> notificationsdata = new List<String>();
foreach(string data in meetingdata)
{
string trimed = data;
//Console.WriteLine(data);
Regex d = new Regex(@"(\d+[\/]\d+[\/]\d+)");
Regex t = new Regex(@"(\d+)([\:])(\d+)\s(PM|AM)");
Match mt =t.Match(trimed);
Match md = d.Match(trimed);
regvalue = md.Value +" " +mt.Value;
DateTime datetime = new DateTime();
if (md.Success && mt.Success)
{
datetime = DateTime.ParseExact(regvalue, "d/M/yyyy hh:mm tt", new CultureInfo("en-US"), DateTimeStyles.None);
//Console.Write(datetime);
}
else { Console.WriteLine("Opps someting Went Wrong Please Contact Developer...!!!"); }//this is not going to happend ever
if (!(datetime < DateTime.Now))
{
if (datetime <= DateTime.Now.AddDays(5))
{
//Console.WriteLine(ctr + " you here");
isNotificationExits = true;
notificationsdata[ctr]=data; <<-----Array Out Of bond Exception here
ctr++;
}
}
}
if(isNotificationExits==true)
{
Console.WriteLine("\n\t\tHey..! You have Some Reminders here\n ");
Console.Write("Sr.no |");
Console.Write(" Subject |");
Console.Write(" Place |");
Console.Write(" Date |");
Console.WriteLine(" Time");
for(int j=0; j <= notificationsdata.Length ; j++)
{
Console.WriteLine(j + 1);
Console.WriteLine(notificationsdata[j]);
}
Console.WriteLine();
}
}
Upvotes: 0
Views: 90
Reputation: 49
If you're using a List, you must add an item to the list using notificationsdata.Add(data)
.
Upvotes: 0
Reputation: 69372
In addition to Nikola's point which you'll also need to fix is that you haven't set a size for the array
string[] notificationsdata = new string[]{};
If you don't know what the size is ahead of time, you can use another data structure such as a List<string>
. I see you've created a List<string>
object but you've named it the same as the array. This is confusing so you should either rename one of them or delete one.
To use the list, replace
notificationsdata[ctr]=data;
with
notificationsdata.Add(data);
Upvotes: 4
Reputation: 2132
You are not specifying where exactly you get the exception, but I would take the (not-so) wild guess and tell you that this piece of code is what bugs you:
for(int j=0; j <= notificationsdata.Length ; j++)
{
Console.WriteLine(j + 1);
Console.WriteLine(notificationsdata[j]);
}
Problem is, you are iterating over the array starting from index zero until index notificationsdata.Length
. However, the Length
method returns the number of items in the array, therefore an index with the array's length wouldn't exist.
What you can do to prevent this is simply change the less-than-or-equal sign to just less-than:
for(int j=0; j < notificationsdata.Length; j++)
or subtract one from the loop maximum:
for(int j=0; j <= notificationsdata.Length - 1; j++)
Upvotes: 1