长度不能为负(例外)

时间:2016-01-25 09:00:33

标签: c#

我一直在对codeeval进行练习: https://www.codeeval.com/open_challenges/30/submit/?lid=1335504

这是我的代码

using System;
using System.Text;
using System.IO;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        using (StreamReader reader = File.OpenText(args[0]))
        while (!reader.EndOfStream)
        {
            StringBuilder result = new StringBuilder();
            string b = "";
            string c = "";
            string[] b2;
            string[] c2;
            int x = 0;
            string line = reader.ReadLine();
            if (null == line)
                continue;
            // do something with line
            else{
            x = line.IndexOf(";");
            b = line.Substring(x +1);
            b2 = b.Split(',');
            c = line.Substring(0, (line.Length - ((b.Length) + 1)));
            c2 = c.Split(',');
            foreach (string u in b2) {
                    foreach (string i in c2)
                    {
                        if (int.Parse(u) == int.Parse(i))
                        {
                            result.Append(u + ",");
                        }
                    }
                }
            Console.WriteLine(result.ToString().Substring(0, result.Length - 1));
            result.Clear();
            Array.Clear(b2, 0 , b2.Length);
            Array.Clear(c2, 0 , c2.Length);
            }
        }
    }
}

当我在我的VS上运行时,它可以在1个字符串上运行。但是,当我在codeeval上运行它时,它运行一行或两行然后给我一个例外。我不知道为什么长度可能是负面的...... ???

  

未处理的异常:System.ArgumentOutOfRangeException:不可以   负。参数名称:System.String.Substring(Int32   startIndex,Int32 length)[0x00000] in:0 at   Program.Main(System.String [] args)[0x00000] in:0   [ERROR]致命的未处理异常:System.ArgumentOutOfRangeException:   不能否定。参数名称:长度at   System.String.Substring(Int32 startIndex,Int32 length)[0x00000] in   :Program.Main为0(System.String [] args)   [0x00000] in:0

输入

68,69,70,71,72,73,74,75,76,77,78,79,80,81,82;78,79,80,81,82,83,84
87,88,89,90,91,92,93;64,65,66,67,68
87,88,89,90,91,92;81,82,83,84,85,86,87
82,83,84,85,86;68,69,70,71,72,73,74,75,76,77,78,79,80
78,79,80,81,82,83,84,85,86,87,88,89,90,91;67,68,69,70,71,72

3 个答案:

答案 0 :(得分:2)

如果所有迭代的if (int.Parse(u) == int.Parse(i))为false,则

result=""将为空。因此,您将获得result.Length - 1以及提取子字符串.Substring(0, result.Length - 1)// points to a negative index which is invalid. 将指向负索引。这是无效的。

if(result.ToString()!="")
{
  //Process your code
}

通过在处理之前检查结果值,可以避免此类错误。即。,

{{1}}

答案 1 :(得分:2)

x = line.IndexOf(";");
b = line.Substring(x +1);
b2 = b.Split(',');
c = line.Substring(0, (line.Length - ((b.Length) + 1)));
  • 您的输入不包含";"
  • 所以x = -1b = line(所以line.Length = b.Length
  • line.Length - ((b.Length) + 1) = -1

因此,请致电Substring,其长度为-1

[更新] 糟糕,刚刚意识到输入中 ;。无论如何,如果你没有验证你的输入,我的例子仍会导致同样的例外。所以我不会删除这个答案,因为它仍然有用。

答案 2 :(得分:1)

考虑这一行:

c = line.Substring(0, (line.Length - ((b.Length) + 1)));

如果line不包含;,则:

  1. x = line.IndexOf(";");会将x设置为-1。
  2. b = line.Substring(x +1);会产生b == line
  3. line.Length - ((b.Length) + 1)会产生负数。
  4. 我想也有其他失败模式。