繁体   English   中英

c#空字符串[]错误

[英]c# empty string[] error

有人请帮我处理这段代码,它显示array outofbond exception

文件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

码:

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

    }

除了Nikola之外,您还需要解决的问题是您尚未设置数组的大小

string[] notificationsdata = new string[]{};

如果您不知道提前的大小,则可以使用其他数据结构,例如List<string> 我看到您创建了一个List<string>对象,但是您将其命名为与数组相同的名称。 这很令人困惑,因此您应该重命名其中之一或删除其中之一。

要使用列表,请替换

notificationsdata[ctr]=data;

notificationsdata.Add(data);

您没有指定要在何处确切地获得异常,但是我会(不是这样)大胆猜测,并告诉您这段代码是您遇到的错误:

for(int j=0; j <= notificationsdata.Length ; j++)
{
    Console.WriteLine(j + 1);
    Console.WriteLine(notificationsdata[j]);
}

问题是,您要从索引0开始遍历数组,直到索引notificationsdata.Length为止。 但是, Length方法返回数组中的项数,因此不存在带有数组长度的索引。

您可以采取的措施只是将小于或等于符号更改为小于:

for(int j=0; j < notificationsdata.Length; j++)

或从循环最大值中减去一个:

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