简体   繁体   English

c#空字符串[]错误

[英]c# empty string[] error

Someone please help me with this code its showing array outofbond exception . 有人请帮我处理这段代码,它显示array outofbond exception

datain file new_hosts.txt is like: 文件new_hosts.txt中的数据类似于:

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();
        }

    }

In addition to Nikola's point which you'll also need to fix is that you haven't set a size for the array 除了Nikola之外,您还需要解决的问题是您尚未设置数组的大小

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> . 如果您不知道提前的大小,则可以使用其他数据结构,例如List<string> I see you've created a List<string> object but you've named it the same as the array. 我看到您创建了一个List<string>对象,但是您将其命名为与数组相同的名称。 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);

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 . 问题是,您要从索引0开始遍历数组,直到索引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. 但是, Length方法返回数组中的项数,因此不存在带有数组长度的索引。

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++)

如果使用列表,则必须使用notificationsdata.Add(data)将项目添加到列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM